使用RxJava+OKhttp3进行图片下载
2016-08-05 17:03
239 查看
1、创建类来初始化一个观察者
2、MainActivity
3、activity_main.xml
package test.ban.com.rx_project;/** * Created by apple on 16/8/5. */ import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import rx.Observable; import rx.Subscriber; /** * 作者:ban on 16/8/5 16:21 */ public class DownloadUtils { private OkHttpClient mOkHttpClient; public DownloadUtils() { mOkHttpClient = new OkHttpClient(); } /** * 声明一个被观察者对象作为结果返回 * * @param path * @return */ public Observable<byte[]> downloadIamge(final String path) { return Observable.create(new Observable.OnSubscribe<byte[]>() { @Override public void call(final Subscriber<? super byte[]> subscriber) { if (!subscriber.isUnsubscribed()) { //访问网络操作 Request request = new Request.Builder().url(path).build(); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { subscriber.onError(e); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { byte[] data = response.body().bytes(); if (data != null) { subscriber.onNext(data); } } subscriber.onCompleted(); } }); } } }); } }
2、MainActivity
package test.ban.com.rx_project; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private Button mButton; private ImageView mImageView; private final String PATH = "https://ss0.bdstatic" + ".com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; private DownloadUtils mDownloadUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDownloadUtils = new DownloadUtils(); mButton = (Button) findViewById(R.id.button); mImageView = (ImageView) findViewById(R.id.imageView2); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //使用http协议获取数据 mDownloadUtils.downloadIamge(PATH) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<byte[]>() { @Override public void onCompleted() { Log.e(TAG, "=====onCompleted====="); //这里一般处理对话框消失 } @Override public void onError(Throwable e) { Log.e(TAG, "onError: ", e); } @Override public void onNext(byte[] bytes) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes .length); mImageView.setImageBitmap(bitmap); } }); } }); } }
3、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="test.ban.com.rx_project.MainActivity"> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="130dp" android:id="@+id/button"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="108dp" android:id="@+id/imageView2"/> </RelativeLayout>
需要的权限:<uses-permission android:name="android.permission.INTERNET"/>
相关文章推荐
- 浅析Node.js实现HTTP文件下载
- 【HTTP】Wireshark过滤规则
- angularjs $http 服务
- python 写的http后台弱口令爆破工具
- TCP服务端收到syn但是不回复syn ack问题分析
- 线程之间的通信实现wc -w
- HttpURLConnection获取图片数据流
- heartbeat httpd nfs 实现高可用web集群
- 【HTTP】WireShark中获取Content-Encoding: gzip时的响应内容
- RBF神经网络
- 【TCP/IP协议】计算机网络中端口号及分类
- iOS开发网络篇—发送json数据给服务器以及多值参数
- Android ImageLoader网络加载图片
- iOS中不能使用HTTP传输数据处理方法
- 自己动手写一个轻量级的Android网络请求框架
- 南京邮电大学的网络攻防训练平台(1)
- android 网络编程--socket tcp/ip udp http之间的关系
- Linux网络编程之简易的点对点聊天程序程序
- TCP的三次握手,四次挥手
- python获取网络时间和本地时间