使用OKhttp只需要做到五个步骤即可,下面通过一个Demo来看看OKhttp的强大之处(最下面有完整代码和详细注释):
一、关联OKhttp框架,添加网络权限
1、jar包准备
官方介绍页面有链接位置。这里把下载链接也写在下面。
okhttp
Okio
2.打开Studio中的清单文件,并添加网络权限。
二、完成Module的布局及控件的初始化
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.xk.okhttptest.MainActivity">
<Button
android:id="@+id/btn_okhttp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="okhttp" />
<ImageView
android:id="@+id/imgShow"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
三、创建OkHttpClient对象及Request设置参数
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(Path)
.build();
四、创建Call对象,调用enqueue方法,开启异步请求
Call call = okHttpClient.newCall(request);
//4.异步请求,请求加入调度
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
//得到从网上获取资源,转换成我们想要的类型
byte[] Picture_bt = response.body().bytes();
//通过handler更新UI
Message message = handler.obtainMessage();
message.obj = Picture_bt;
message.what = SUCCESS;
handler.sendMessage(message);
}
});
五、创建Handler,完成对下载的字符串进行UI更新。
private Handler handler = new Handler( ){
public void handleMessage(Message msg) {
switch (msg.what) {
//加载网络成功进行UI的更新,处理得到的图片资源
case SUCCESS:
//通过message,拿到字节数组
byte[] Picture = (byte[]) msg.obj;
//使用BitmapFactory工厂,把字节数组转化为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
//通过imageview,设置图片
imgShow.setImageBitmap(bitmap);
break;
//当加载网络失败执行的逻辑代码
case FALL:
Toast.makeText(MainActivity.this, "网络出现了问题", Toast.LENGTH_SHORT).show();
break;
}
super.handleMessage(msg);
}
};
完整的代码:
public class MainActivity extends AppCompatActivity {
private Button btn_okhttp;
private ImageView imgShow;
private static final int SUCCESS = 1;//成功
private static final int FALL = 2;//失败
private Handler handler = new Handler( ){
public void handleMessage(Message msg) {
switch (msg.what) {
//加载网络成功进行UI的更新,处理得到的图片资源
case SUCCESS:
//通过message,拿到字节数组
byte[] Picture = (byte[]) msg.obj;
//使用BitmapFactory工厂,把字节数组转化为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
//通过imageview,设置图片
imgShow.setImageBitmap(bitmap);
break;
//当加载网络失败执行的逻辑代码
case FALL:
Toast.makeText(MainActivity.this, "网络出现了问题", Toast.LENGTH_SHORT).show();
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_okhttp = (Button) findViewById(R.id.btn_okhttp);
imgShow = (ImageView) findViewById(R.id.imgShow);
/*okHttpClient = new OkHttpClient();*/
btn_okhttp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1.创建一个okhttpclient对象
OkHttpClient okHttpClient = new OkHttpClient();
//2.创建Request.Builder对象,设置参数,请求方式如果是Get,就不用设置,默认就是Get
Request request = new Request.Builder()
//https://www.baidu.com/img/bd_logo1.png
.url("https://www.baidu.com/img/bd_logo1.png")
.build();
//3.创建一个Call对象,参数是request对象,发送请求
Call call = okHttpClient.newCall(request);
//4.异步请求,请求加入调度
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
//得到从网上获取资源,转换成我们想要的类型
byte[] Picture_bt = response.body().bytes();
//通过handler更新UI
Message message = handler.obtainMessage();
message.obj = Picture_bt;
message.what = SUCCESS;
handler.sendMessage(message);
}
});
}
});
}
}
执行后的效果: