当前位置: 代码迷 >> 综合 >> 【Java】okHttp3 简单使用
  详细解决方案

【Java】okHttp3 简单使用

热度:19   发布时间:2024-02-05 00:37:56.0

之前用的RestTemplate,发现一个multipart的http请求始终发不成功,后面就试了下okHttp,发现真的好用,api太清爽了,记录一下使用:

package com.liyao;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import org.jetbrains.annotations.NotNull;
import org.junit.Test;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;public class OhHttpTest {private static final String host = "http://localhost.charlesproxy.com:8080";// 最好用单例private static OkHttpClient okHttpClient = new OkHttpClient();@Testpublic void post1() throws IOException {// RequestBody 有三种构建方式:// 1.  FormBody.Builder,构建application/x-www-form-urlencoded类型的post表单// 2.  MultipartBody.Builder,构建multipart类型的body,多个内容合成到一个body里// 3.  RequestBody.create方法构建body体,传入两个参数:类型和内容RequestBody body = new FormBody.Builder().add("key1", "value1").add("key1", "value1&key2=value2").addEncoded("key3", "value3?&=") // 这俩方法没看出啥区别,都会urlencode.build();Request request = new Request.Builder().url(host + "/rest/n/testApi/path").post(body).build();Call call = okHttpClient.newCall(request);call.execute();}@Testpublic void post2() throws IOException {RequestBody body = RequestBody.create("{}", MediaType.parse("application/json"));Request request = new Request.Builder().url(host + "/rest/n/testApi/path").post(body).build();Call call = okHttpClient.newCall(request);call.execute();}@Testpublic void multipart() throws IOException {InputStream inputStream = new FileInputStream("src/test/resources/mapping.xlsx");byte[] bs = inputStream.readAllBytes();// 随便读一个excel文件,当做二进制流处理呗RequestBody part1 = RequestBody.create(bs, MediaType.parse("application/octet-stream"));// 通过构造方法指定boundary,一般不需要,只是知道有这么个用法RequestBody multipart = new MultipartBody.Builder("MyBoundary").setType(MultipartBody.MIXED).addFormDataPart("m1", "v1").addFormDataPart("m2", "v2", part1).build();Request request = new Request.Builder().url(host + "/rest/n/testApi/path").post(multipart).build();Call call = okHttpClient.newCall(request);call.execute();}@Testpublic void get1() {// get没有bodyRequest request = new Request.Builder().url(host + "/rest/n/testApi/hello").get().build();Call call = okHttpClient.newCall(request);// synctry {Response response = call.execute();// process response} catch (IOException e) {e.printStackTrace();}}@Testpublic void get2() {Request request = new Request.Builder().url(host + "/rest/n/testApi/hello").get().build();Call call = okHttpClient.newCall(request);// asynccall.enqueue(new Callback() {@Overridepublic void onFailure(@NotNull Call call, @NotNull IOException e) {// process error}@Overridepublic void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {// process response}});// wait async resulttry {Thread.sleep(100000);} catch (InterruptedException e) {e.printStackTrace();}}
}

 

  相关解决方案