继续使用上面的项目
1.被调用的项目
package com.jun.web2forokhttp.okhttp; import com.jun.web2forokhttp.bean.HttpDomain; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; @RestController public class PostReq { @PostMapping(value = "/post/getInfo") @ResponseBody public HttpDomain getInfo(@RequestBody HttpDomain httpDomain){ HttpDomain useHttpDomain =new HttpDomain(); useHttpDomain.setType(httpDomain.getType()+"-post"); useHttpDomain.setAge(httpDomain.getAge()+10); return useHttpDomain; } }
bean:
package com.jun.web2forokhttp.bean; import lombok.Data; @Data public class HttpDomain { private String type; private String name; private String age; }
2.调用的程序
package com.jun.web.okhttp; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jun.web.okhttp.bean.HttpDomain; import com.jun.web.okhttp.bean.HttpResponseDomain; import okhttp3.*; import java.io.IOException; public class PostHttp { public static void main(String[] args) throws JsonProcessingException { formBody(); } /** * 没有请求头的请求 */ public static void formBody() throws JsonProcessingException { HttpDomain httpDomain = new HttpDomain(); httpDomain.setType("1"); httpDomain.setName("tom"); httpDomain.setAge(10); ObjectMapper objectMapper = new ObjectMapper(); String Json=objectMapper.writeValueAsString(httpDomain); //转JSON String url = "http://localhost:8080/post/getInfo"; OkHttpClient okHttpClient = new OkHttpClient(); MediaType json = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(json,Json); Request request = new Request.Builder() .url(url) .post(body) .build(); Call call = okHttpClient.newCall(request); try{ Response response = call.execute(); String result = response.body().string(); System.out.println("post="+result); HttpResponseDomain httpResponseDomain = objectMapper.readValue(result,HttpResponseDomain.class); //转对象 System.out.println("httpResponseDomain="+httpResponseDomain); } catch (IOException e) { e.printStackTrace(); } } }
需要的bean
package com.jun.web.okhttp.bean; import lombok.Data; import java.io.Serializable; @Data public class HttpDomain implements Serializable{ private String type; private String name; private int age; }
package com.jun.web.okhttp.bean; import lombok.Builder; import lombok.Data; @Data @Builder public class HttpResponseDomain { private String type; private String age; private String name; public HttpResponseDomain(){} public HttpResponseDomain(String type,String age,String name){ this.type=type; this.age=age; this.name=name; } // private String name; }
3.效果
二:注意点
1.RequestBody的数据格式
常见的content-type主要有三种
application/x-www-form-urllencoded
application/form-data
application/json
上文的示例就是json格式。
2.如果数据包含文件
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
.build();
三:同步与异步
1.异步
package com.jun.web.okhttp; import okhttp3.*; import java.io.IOException; public class Asyn { public static void main(String[] args) { String url = "https://www.baidu.com/"; OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { System.out.println("我是异步线程,线程Id为:" + Thread.currentThread().getId()); } }); for (int i = 0; i < 10; i++) { System.out.println("我是主线程,线程Id为:" + Thread.currentThread().getId()); try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
效果