OkHttp使用及拦截器

准备

使用前,对于Android Studio,选择在Gradle添加:

1
compile 'com.squareup.okhttp3:okhttp:(insert latest version)'

使用

GET A URL

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
//某个控件的点击监听器
private View.OnClickListener mDownloadClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://liwenquan.top")
.build();
okHttpClient.newCall(request).enqueue(callback);
}
};
//请求后的回调接口
private Callback callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("LWQ", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//onResponse|onFailure都不是在UI线程中执行,因此进行UI相关的操作,需要在UI线程中进行,这里借助了Handler
Message msg = new Message();
msg.what = 0x123;
msg.obj = response.body().string();
mHandler.sendMessage(msg);
}
};
}

POST TO A SERVER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}