<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”
<Button
android:id=“@+id/btn”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“点击下载” />
<ImageView
android:id=“@+id/iview”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:src=“@mipmap/ic_launcher”/>
<ProgressBar
android:id=“@+id/bar”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:max=“100”
style=“@android:style/Widget.ProgressBar.Horizontal”/>
下面就是今天的重头戏——okhttp了。下面请看代码;
package com.example.acer_pc.threedownload;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView imageView;
private ProgressBar bar;
//声明Handler对象,进行对ui的更改
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bitmap bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用查找控件的方法
initFindView();
//button按钮的监听事件,当点击按钮时,实现progressbar进度条消失并下载图片
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downLoad();
for (int i = 0; i <= 100 ; i++) {
bar.setProgress(i);
}
//进度条消失的方法
bar.setVisibility(View.GONE);
}
private void downLoad() {
//创建客户端对象
OkHttpClient client = new OkHttpClient();
//创建请求对象
Request request = new Request.Builder().url(“http://img04.taobaocdn.com/bao/uploaded/i4/15639020891997878/T12_pTXrlXXXXXXXXX_!!0-item_pic.jpg”).build();
//创建call对象,请求网络,并且将数据返回
Call call = client.newCall(request);
//执行请求
call.enqueue(new Callback() {
//请求失败时的方法
@Override
public void onFailure(Request request, IOException e) {
System.out.println(e.toString());
}
//请求成功时的方法
@Override
public void onResponse(Response response) throws IOException {
//网络请求成功时的反应对象
ResponseBody body = response.body();
//通过body对象获取网络数据
//InputStream inputStream = body.byteStream();
//获取读取流的长度,表示获取数据的总的大小
// long length = body.contentLength();
byte[] bytes = body.bytes();
//通过位图加工厂,将所下载的图片转换为字节发送到主线程
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
if (bitmap != null){
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
}
}
});