做Cookie的持久化。 OKHttp3.0之后和之前做Cookie持久化有了点区别下面直接上代码:
public static String httpPost(String url,String json) {
String res = "";
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().cookieJar(new CookieJar() {
private final HashMap<String, List> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl httpUrl, List cookies) {
cookieStore.put(httpUrl.host(), cookies);
}
@Override
public List loadForRequest(HttpUrl httpUrl) {
List cookies = cookieStore.get(httpUrl.host());
return cookies != null ? cookies : new ArrayList();
}
}).connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).build();
RequestBody requestBody = RequestBody.create(JSON, json);
//创建一个请求对象
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
//发送请求获取响应
try {
Response response=okHttpClient.newCall(request).execute();
res = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}