master
parent
485e1d47a9
commit
aa3280a473
@ -1,45 +0,0 @@
|
|||||||
package com.yhjd.util;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class OkHttpUtils {
|
|
||||||
|
|
||||||
public static JSONObject response(OkHttpClient client, Request request){
|
|
||||||
// 4 执行请求操作
|
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
|
|
||||||
System.out.println(response);
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
String string = response.body().string();
|
|
||||||
System.out.println("返回内容"+string);
|
|
||||||
JSONObject result = JSONObject.parseObject(string);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("请求微服务失败",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String stringResponse(OkHttpClient client, Request request){
|
|
||||||
// 4 执行请求操作
|
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
|
|
||||||
System.out.println(response);
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
String string = response.body().string();
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("请求微服务失败",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue