OKHttpUtils.java
7.07 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.huaheng.wms.https;
import android.util.Log;
import com.huaheng.wms.WMSApplication;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
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 getAsycHttp(String url) {
OkHttpClient mOkHttpClient = new OkHttpClient();
Request.Builder requestBuilder = new Request.Builder().url(url);
requestBuilder.method("GET",null);
Request request = requestBuilder.build();
Call mcall= mOkHttpClient.newCall(request);
mcall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (null != response.cacheResponse()) {
String str = response.cacheResponse().toString();
Log.i(TAG, "cache---" + str);
} else {
response.body().string();
String str = response.networkResponse().toString();
Log.i(TAG, "network---" + str);
}
}
});
}
public static void commonPost(String url, RequestBody formBody) {
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
.readTimeout(HttpConstant.READ_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HttpConstant.WRITE_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(HttpConstant.CONNECT_TIMEOUT, TimeUnit.SECONDS).build();
Request request=null;
String cookie = WMSApplication.getOkhttpCookie();
if(cookie == null) {
request = new Request.Builder()
.url(url)
.post(formBody)
.build();
} else {
request = new Request.Builder()
.addHeader("cookie",cookie)
.url(url)
.post(formBody)
.build();
}
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("WeiPos", "onFailure :" + call.toString());
e.printStackTrace();
}
@Override
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(";"));
Log.i("WeiPos", "cookies:" + result);
WMSApplication.setOkhttpCookie(result);
}
Log.i("WeiPos", str);
}
});
}
private void downAsynFile() {
OkHttpClient mOkHttpClient = new OkHttpClient();
String url = "http://img.my.csdn.net/uploads/201603/26/1458988468_5804.jpg";
Request request = new Request.Builder().url(url).build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) {
InputStream inputStream = response.body().byteStream();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(new File("/sdcard/wangshu.jpg"));
byte[] buffer = new byte[2048];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
} catch (IOException e) {
Log.i(TAG, "IOException");
e.printStackTrace();
}
Log.d(TAG, "文件下载成功");
}
});
}
private void postAsynFile() {
OkHttpClient mOkHttpClient=new OkHttpClient();
File file = new File("/sdcard/wangshu.txt");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, response.body().string());
}
});
}
public static void sendMultipart(){
OkHttpClient mOkHttpClient = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "wangshu")
.addFormDataPart("image", "wangshu.jpg",
RequestBody.create(MEDIA_TYPE_PNG, new File("/sdcard/wangshu.jpg")))
.build();
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + "...")
.url("https://api.imgur.com/3/image")
.post(requestBody)
.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, response.body().string());
}
});
}
private void setConfig() {
File sdcache = new File("/sdcard");
int cacheSize = 10 * 1024 * 1024;
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
OkHttpClient mOkHttpClient=builder.build();
}
}