OKHttp是一款高效的HTTP客户端,支持连接同一地址的链接共享同一个socket,通过连接池来减小响应延迟,还有透明的GZIP压缩,请求缓存等优势,其核心主要有路由、连接协议、拦截器、代理、安全性认证、连接池以及网络适配,拦截器主要是指添加,移除或者转换请求或者回应的头部信息
这个库也是square开源的一个网络请求库(okhttp内部依赖okio)因此,使用okhttp框架时除了下载okhttp的jar包还需要下载okio的jar包。
下载地址:http://square.github.io/okhttp/
- 拦截器
代码展示:
package com.example.dell.myapplication;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_get;
private Button btn_post;
private TextView tv_result;
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");//配置json的编码
/**
* get请求
*/
private static final int GET = 1;
/**
* post请求
*/
private static final int POST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get = (Button) findViewById(R.id.btn_get);
btn_post = (Button) findViewById(R.id.btn_post);
tv_result = (TextView) findViewById(R.id.tv_result);
//设置点击事件
btn_get.setOnClickListener(this);
btn_post.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_get:
getDataFromByGet();
break;
case R.id.btn_post:
getDataFromByPost();
break;
}
}
private void getDataFromByGet() {
//必须在子线程中请求
new Thread(){
@Override
public void run() {
super.run();
try {
String resutl=get("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
Message msg=Message.obtain();
msg.what=GET;
msg.obj=resutl;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
OkHttpClient client=new OkHttpClient();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case GET://get请求
tv_result.setText(msg.obj.toString());
break;
case POST://post请求
tv_result.setText(msg.obj.toString());
break;
}
}
};
private String get(String s) throws IOException {
Request request=new Request.Builder()
.url(s)
.build();
Response response=client.newCall(request).execute();
return response.body().string();
}
private void getDataFromByPost() {
new Thread(){
@Override
public void run() {
super.run();
try {
String resutl=post("http://api.m.mtime.cn/PageSubArea/TrailerList.api","");
Message msg=Message.obtain();
msg.what=POST;
msg.obj=resutl;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private 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();
}
}
运行截图: