OKHttpUtils.java
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.huaheng.download;
import com.huaheng.common.WMSLog;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by youjie on 2018/4/27.
*/
public class OKHttpUtils {
private static final String TAG = "HttpUtils";
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
public static void commonPost(String url, String jsonString, final OKHttpInterface okHttpInterface) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(JSON, jsonString);
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS).build();
Request request= null;
request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
WMSLog.d( "onFailure :" + e.toString());
okHttpInterface.onFail(e.toString());
}
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie");
if (cookies.size() > 0) {
String session = cookies.get(0);
String result = session.substring(0, session.indexOf(";"));
System.out.println( "cookies:" + result);
}
okHttpInterface.onSuccess(str);
WMSLog.d("onResponse:" + str);
}
});
}
}