Nohttp 获取图片Bitmap

来源:互联网 发布:dijkstra算法 填空 编辑:程序博客网 时间:2024/05/16 15:21

Nohttp请求图片的两种简答的方式:普通请求以及缓存请求

开局声明:这是基于nohttp1.0.4-include-source.jar版本写的教程

由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去请求网络将还会报错:

onFailed: com.yolanda.nohttp.error.NetworkError: The network is not available, please check the network. The requested url is: http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20

首先是初始化整个应用全局的请求队列

复制代码
 1 package com.qg.lizhanqi.nohttpdemo; 2  3 import android.app.Application; 4  5 import com.yolanda.nohttp.NoHttp; 6  7 /** 8  * Created by lizhanqi on 2016-7-28-0028. 9  */10 public class MyApplication extends Application {11     @Override12     public void onCreate() {13         //对你没看错就是这么一行就这么简单,NOhttp就是这么简单14         NoHttp.initialize(this);15         super.onCreate();16     }17 }
复制代码

 

//普通的请求

复制代码
 1     public void noHttpLoadImag(String url) { 2         //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) 3         requestQueues = NoHttp.newRequestQueue(); 4         //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式) 5         final Request<Bitmap> imageRequest = NoHttp.createImageRequest(url);//这里 RequestMethod.GET可以不写(删除掉即可),默认的是Get方式请求 6         //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调 7         requestQueues.add(4, imageRequest, new SimpleResponseListener<Bitmap>() { 8             @Override//成功后的回调 9             public void onSucceed(int i, Response<Bitmap> response) {10                 imageView.setImageBitmap(response.get());11             }12 13             @Override//失败后的回调14             public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {15             }16         });17     }
复制代码

//带有缓存的请求图片

复制代码
 1     public void noHttpLoadCacheImag(String url) { 2         //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) 3         requestQueues = NoHttp.newRequestQueue(); 4         //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式) 5         Request<Bitmap> imageRequest = NoHttp.createImageRequest(url);//这里 RequestMethod.GET可以不写(删除掉即可),默认的是Get方式请求 6         //第三步:设置请求缓存的五种模式:(这里与文字缓存一样) 7         //DEFAULT是http标准协议的缓存 8         //imageRequest.setCacheMode(CacheMode.DEFAULT); 9         //REQUEST_NETWORK_FAILED_READ_CACHE请求失败返回上次缓存的数据(建议使用这种)10         imageRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);11         //NONE_CACHE_REQUEST_NETWORK在没有缓存再去请求网络12         // imageRequest.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK);13         // ONLY_READ_CACHE仅仅请求缓存,如果没有缓存就会请求失败14         //imageRequest.setCacheMode(CacheMode.ONLY_READ_CACHE);15         //ONLY_REQUEST_NETWORK仅仅请求网络不支持302重定向16         // imageRequest.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);17 18         //第四步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调19         requestQueues.add(5, imageRequest, new SimpleResponseListener<Bitmap>() {20             @Override//请求成功的回调21             public void onSucceed(int i, Response<Bitmap> response) {22                 imageView.setImageBitmap(response.get());23             }24             @Override//请求失败的回调25             public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {26             }27         });28     }
0 0