OKHttp是一个非常成熟,高效的网络请求框架。目前最新版本是OKHttp3。之前的版本相比变化很大,感兴趣的伙伴可以Google或Baidu一下。
项目开源地址:http://square.github.io/okhttp/
OKHttp的主要优势有:
- 支持 SPDY ,共享同一个Socket来处理相同服务器的所有请求,如SPDY不可用,则通过连接池来减少请求延时。
- 无缝的支持GZIP来减少数据流量。
- 缓存响应数据来减少重复的网络请求等。
对我们Android开发者来说,使用OKHttp框架非常方便。AndroidStudio项目对应build.gradle文件添加 :
compile ‘com.squareup.okhttp3:okhttp:3.4.1’
代码如下:
dependencies {
compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
接下来就可以直接在项目中使用OKHttp来进行网络请求了。
—1.OKHttp之GET请求——-
//通过Request构造Http请求:需要传入请求地址url
Request request = new Request.Builder().url(GET_URL).build();
//创建OkHttpClient对象:目的是通过OkHttpClient初始化Call对象
OkHttpClient client = new OkHttpClient();
//通过初始化Call对象,来实现网络连接
Call call = client.newCall(request);
//网络请求回调,回调方法是在非UI线程进行的
Callback callback = new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {//请求成功
Message msg = new Message();
//通过Response获得网络资源,以字符串(Android通常是json格式)的形式获得网络请求响应的数据
msg.obj = response.body().toString();
mHandler.sendMessage(msg);
}
@Override
public void onFailure(Call call, IOException e) {//请求失败
mHandler.sendEmptyMessage(0);
}
};
call.enqueue(callback);
–2.OKHttp之POST请求——–
//创建FormBody.Builder对象,用于封闭Post请求参数
FormBody.Builder builder = new FormBody.Builder();
//添加参数信息
builder.add("userName", "xxx");
builder.add("passWord", "xxx");
//通过Request构造Http请求:需要传入请求地址url及参数信息
Request request = new Request.Builder().url(POST_URL).post(builder.build()).build();
//创建OkHttpClient对象:目的是通过OkHttpClient初始化Call对象
OkHttpClient client = new OkHttpClient();
//通过初始化Call对象,来实现网络连接
Call call = client.newCall(request);
//网络请求回调,回调方法是在非UI线程进行的
Callback callback = new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {//请求成功
Message msg = new Message();
//通过Response获得网络资源,以字符串(Android通常是json格式)的形式获得网络请求响应的数据
msg.obj = response.body().toString();
mHandler.sendMessage(msg);
}
@Override
public void onFailure(Call call, IOException e) {//请求失败
mHandler.sendEmptyMessage(0);
}
};
call.enqueue(callback);
完整代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_get, btn_post;
//下面是GET和POST请求地址,随便写的,请勿对号入座
private final static String GET_URL = "http://www.baidu.com";
private final static String POST_URL = "http://fc.i2b.mobile/login.json";
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (TextUtils.isEmpty(msg.obj.toString())) {
//处理响应结果:比如在界面上展示数据
Toast.makeText(MainActivity.this, "请求响应结果 :" + msg.obj.toString(), Toast.LENGTH_LONG).show();
}
if (msg.what == 0) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViewsAndEvents();
}
private void initViewsAndEvents() {
btn_get = (Button) findViewById(R.id.btn_get);
btn_post = (Button) findViewById(R.id.btn_post);
btn_post.setOnClickListener(this);
btn_get.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get:
okHttpGet();
break;
case R.id.btn_post:
okHttpPost();
break;
}
}
/**
* @param
* @description okHttp网络请求GET方式
* @author ldm
* @time 2016/10/22 16:12
*/
private void okHttpGet() {
//通过Request构造Http请求:需要传入请求地址url
Request request = new Request.Builder().url(GET_URL).build();
//创建OkHttpClient对象:目的是通过OkHttpClient初始化Call对象
OkHttpClient client = new OkHttpClient();
//通过初始化Call对象,来实现网络连接
Call call = client.newCall(request);
//网络请求回调,回调方法是在非UI线程进行的
Callback callback = new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {//请求成功
Message msg = new Message();
//通过Response获得网络资源,以字符串(Android通常是json格式)的形式获得网络请求响应的数据
msg.obj = response.body().toString();
mHandler.sendMessage(msg);
}
@Override
public void onFailure(Call call, IOException e) {//请求失败
mHandler.sendEmptyMessage(0);
}
};
call.enqueue(callback);
}
/**
* @param
* @description okHttp网络请求POST方式
* @author ldm
* @time 2016/10/22 16:12
*/
private void okHttpPost() {
//创建FormBody.Builder对象,用于封闭Post请求参数
FormBody.Builder builder = new FormBody.Builder();
//添加参数信息
builder.add("userName", "xxx");
builder.add("passWord", "xxx");
//通过Request构造Http请求:需要传入请求地址url及参数信息
Request request = new Request.Builder().url(POST_URL).post(builder.build()).build();
//创建OkHttpClient对象:目的是通过OkHttpClient初始化Call对象
OkHttpClient client = new OkHttpClient();
//通过初始化Call对象,来实现网络连接
Call call = client.newCall(request);
//网络请求回调,回调方法是在非UI线程进行的
Callback callback = new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {//请求成功
Message msg = new Message();
//通过Response获得网络资源,以字符串(Android通常是json格式)的形式获得网络请求响应的数据
msg.obj = response.body().toString();
mHandler.sendMessage(msg);
}
@Override
public void onFailure(Call call, IOException e) {//请求失败
mHandler.sendEmptyMessage(0);
}
};
call.enqueue(callback);
}
}