OkHttps工具类中主要封装了get与post请求,这里简单让大家看下,源代码会贴在文章末尾
/*** OkHttpUtils 工具类
*/
@Slf4j
public class OkHttpUtils {
private final static OkHttpClient client = new OkHttpClient().newBuilder()
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
public static JsonObject doGetRetJson(String url) throws IOException {
return doGetRetJsonElement(url).getAsJsonObject();
}
public static JsonElement doGetRetJsonElement(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
return sendSyncRequestRetJsonElement(request);
}
public static JsonObject doPostRetJson(String url, String jsonBody) throws IOException {
return doPostRetJsonElement(url, jsonBody).getAsJsonObject();
}
public static JsonElement doPostRetJsonElement(String url, String jsonBody) throws IOException {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody);
Request request = new Request.Builder().url(url).post(requestBody).build();
return sendSyncRequestRetJsonElement(request);
}
public static String doPostRetJsonString(String url, String jsonBody) throws IOException {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody);
Request request = new Request.Builder().url(url).post(requestBody).build();
return sendSyncRequestRetString(request);
}
public static String doPostRetString(String url, String textBody) throws IOException {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), textBody);
Request request = new Request.Builder().url(url).post(requestBody).build();
return sendSyncRequestRetString(request);
}
public static void doPost(String url, Map<String, String> params) throws IOException {
FormBody.Builder formBuilder = new FormBody.Builder();
for (String key : params.keySet()) {
formBuilder.add(key, params.get(key));
}
Request request = new Request.Builder().url(url).post(formBuilder.build()).build();
sendSyncRequestRetString(request);
}
public static JsonObject doPostRetJson(String url, Map<String, String> params) throws IOException {
FormBody.Builder formBuilder = new FormBody.Builder();
for (String key : params.keySet()) {
formBuilder.add(key, params.get(key));
}
Request request = new Request.Builder().url(url).post(formBuilder.build()).build();
return sendSyncRequestRetJson(request);
}
/**
* 支付宝请求返回form
*
* @param url
* @param params
* @return
* @throws IOException */
public static String doPostResString(String url, Map<String, String> params) throws IOException {
FormBody.Builder formBuilder = new FormBody.Builder();
for (String key : params.keySet()) {
formBuilder.add(key, params.get(key));
}
Request request = new Request.Builder().url(url).post(formBuilder.build()).build();
return sendSyncRequestRetString(request);
}
public static JsonObject doPostRetJsonForUploadFile(String url, Map<String, Object> params) throws IOException {
MultipartBody.Builder formBuilder = new MultipartBody.Builder();
formBuilder.setType(MultipartBody.FORM);
params.forEach((key, value) -> {
if (value instanceof File) {
File file = (File) value;
formBuilder.addFormDataPart(key, file.getName(), RequestBody.create(MediaType.parse("application/pdf; charset=utf-8"), file));
} else {
formBuilder.addFormDataPart(key, value.toString());
}
});
Request request = new Request.Builder().url(url).post(formBuilder.build()).build();
return sendSyncRequestRetJson(request);
}
private static JsonObject sendSyncRequestRetJson(Request request) throws IOException {
return sendSyncRequestRetJsonElement(request).getAsJsonObject();
}
private static JsonElement sendSyncRequestRetJsonElement(Request request) throws IOException {
return JsonUtils.toJsonElement(sendSyncRequestRetString(request));
}
private static String sendSyncRequestRetString(Request request) throws IOException {
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("请求出错,出错信息:" + response.message());
}
String retString = response.body().string();
if (StrUtil.isEmpty(retString)) {
return "";
}
return retString;
}
}
kHttps工具类引入完成之后,引入JsonUtils工具类,这个主要是帮助我们吧参数封给JSON,免去我们在每个接口中再进行单独封装
/*** 参数转json工具类
*/
public class JsonUtils {
private static final JsonParser JSON_PARSER = new JsonParser();
public static String toJson(Object object) {
Gson gson = new GsonBuilder().create();
return gson.toJson(object);
}
public static JsonObject toJsonObject(String json) {
return JSON_PARSER.parse(json).getAsJsonObject();
}
public static JsonArray jsonObjectToJsonArray(JsonObject jsonObject) {
JsonArray jsonArray = new JsonArray();
jsonArray.add(jsonObject);
return jsonArray;
}
public static JsonElement toJsonElement(String json) {
return JSON_PARSER.parse(json);
}
}
工具类都引入完毕之后,就是我们的重头戏了
接口开发第一步,抽离接口Api工具类