繼續使用上面的項目javascript
1.被調用的項目java
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:web
package com.jun.web2forokhttp.bean; import lombok.Data; @Data public class HttpDomain { private String type; private String name; private String age; }
2.調用的程序spring
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(); } } }
須要的beanjson
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.效果app
二:注意點異步
1.RequestBody的數據格式ide
常見的content-type主要有三種post
application/x-www-form-urllencodedui
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(); } } } }
效果