首页 volley网络访问框架

volley网络访问框架

举报
开通vip

volley网络访问框架volley网络访问框架 1. 什么是Volley 2. 来源: 3. 附录、参考link: 1. Volley主页 演讲 ;feature=player_embedded 3. Android Tips; Android : Volley: Easy, Fast Networking for Android 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(Apache)等,今年的G...

volley网络访问框架
volley网络访问框架 1. 什么是Volley 2. 来源: 3. 附录、参考link: 1. Volley主页 演讲 ;feature=player_embedded 3. Android Tips; Android : Volley: Easy, Fast Networking for Android 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(Apache)等,今年的Google I/O 2013上,Volley发布了。Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。 比如以前从网上下载图片的步骤可能是这样的 流程 快递问题件怎么处理流程河南自建厂房流程下载关于规范招聘需求审批流程制作流程表下载邮件下载流程设计 : , 在ListAdapter#getView()里开始图像的读取。 , 通过AsyncTask等机制使用HttpURLConnection从服务器去的图片资源 , 在AsyncTask#onPostExecute()里设置相应ImageView的属性。 而在Volley下,只需要一个函数即可,详细见后面的例子。 简单来说,它提供了如下的便利功能: , JSON,图像等的异步下载; , 网络请求的排序(scheduling) , 网络请求的优先级处理 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts , 缓存 , 多级别取消请求 , 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求) 3.1. 最简单的get请求 这个例子很简单,从网络取得JSON对象,然后打印出来。 [java] view plaincopy , mQueue = Volley.newRequestQueue(getApplicationContext()); , mQueue.add(new JsonObjectRequest(Method.GET, url, null, , new Listener() { , @Override , public void onResponse(JSONObject response) { , Log.d(TAG, "response : " + response.toString()); , } , }, null)); ? mQueue.start(); 3.2. 给ImageView设置图片源 [java] view plaincopy ,, // imageView是一个ImageView实例 ,, // ImageLoader.getImageListener的第二个参数是默认的图片resource id ,, // 第三个参数是请求失败时候的资源id,可以指定为0 ,, ImageListener listener = ImageLoader.getImageListener(imageView, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete); ,, mImageLoader.get(url, listener); ImageLoader的方法都需要从主线程里来调用。 3.3. 使用NetworkImageView advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts Volley提供了一个新的控件NetworkImageView来代替传统的ImageView,这个控件的图片属性可以通过 [java] view plaincopy ,, mImageView.setImageUrl(url, imageLoader) 如果在一个Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,如果Activity被结束了,则我们需要写如下代码作为防守: [java] view plaincopy , @Override public void onPostExecute(Result r) { , if (getActivity() == null) { , return; , } , // ... , } Activity被终止之后,如果继续使用其中的Context等,除了无辜的浪费CPU,电池,网络等资源,有可能还会导致程序crash,所以,我们需要处理这种一场情况。 使用Volley的话,我们可以在Activity停止的时候,同时取消所有或部分未完成的网络请求。 Volley里所有的请求结果会返回给主进程,如果在主进程里取消了某些请求,则这些请求将不会被返回给主线程。 比如,可以针对某些个request做取消操作: [java] view plaincopy , @Override , public void onStop() { advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts ? for (Request req : mInFlightRequests) { ,, req.cancel(); ,, } ,, ... ,, } 或者,取消这个队列里的所有请求: [java] view plaincopy ,, @Override pubic void onStop() { ,, mRequestQueue.cancelAll(this); ,, ... ,, } 也可以根据RequestFilter或者Tag来终止某些请求: [java] view plaincopy ,, @Override public void onStop() { ,? mRequestQueue.cancelAll( new RequestFilter() {}) ,, ... ,, // or ,, mRequestQueue.cancelAll(new Object()); ,, ... 谷歌Volley网络框架讲解——Network及其实现类 我们看到Network接口只有一个实现类BasicNetwork,而HttpStack有两个实现类。 BasicNetwork这个类是toolbox工具箱包里的,实现了Network接口。 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 先来看下Network这个interface,performRequest(Request*)执行一个请求,以一个Request为参数,返回一个 NetworkResponse 。 public interface Network { /** * Performs the specified request.执行这个请求 * @param request Request to process//待处理的请求 * @return A {@link NetworkResponse} with data and caching metadata; will never be null * 返回一个请求结果,不会为空 * @throws VolleyError on errors */ public NetworkResponse performRequest(Request request) throws VolleyError; } 再来看下它的构造函数,两个参数HttpStack和ByteArrayPool,这两个参数就是主要的成员变量。 /** * 带一个默认大小的ByteArrayPool缓冲池 * @param httpStack HTTP stack to be used */ public BasicNetwork(HttpStack httpStack) { // If a pool isn't passed in, then build a small default pool that will give us a lot of // benefit and not use too much memory. //如果一个池没有通过,将建立一个小的默认缓存池,这样会给我们带来很大的益处,不需要耗费很多内存 this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE)); } /** * 主构造方法BasicNetwork(HttpStack*,ByteArrayPool*) advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts * @param httpStack HTTP stack to be used * @param pool a buffer pool that improves GC performance in copy operations */ public BasicNetwork(HttpStack httpStack, ByteArrayPool pool) { mHttpStack = httpStack; mPool = pool; } 再看看哪个方法用到了mHttpStack,就是在实现Network接口的performRequest()方法,并且mHttpStack有个跟Network接口同名的方法,这才是真正执行请求的方法,也是直接传入请求返回响应。 而mPool是在entityToBytes()这个方法中用到,顾名思义这个方法就是把HttpEntity转换为bytes数据,而这个缓存池就是为便捷转换数据格式。 再详细看下最重要的方法performRequest(),代码中均以加上注释,见解有误望读者们见谅和请教。 /** * @title performRequest执行各种Request请求并以NetworkResponse的形式返回结果 * @param Request * @return NetworkResponse * @throws VolleyError * 定义:{@link Network#performRequest(Request)} * 被调:{@link NetworkDispatcher#run()} * */ @Override//NetworkDispatcher的run()方法中调用 public NetworkResponse performRequest(Request request) throws VolleyError { long requestStart = SystemClock.elapsedRealtime();//开始请求时间 while (true) { HttpResponse httpResponse = null;//apache的请求结果 byte[] responseContents = null;//请求的内容 Map responseHeaders = new HashMap();//响应结果头部信息 try { // Gather headers. Map headers = new HashMap();//保存缓存数据 addCacheHeaders(headers, request.getCacheEntry());//先获取缓存数据 httpResponse = mHttpStack.performRequest(request, headers);//去调advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 用mHttpStack的实现方法执行请求 StatusLine statusLine = httpResponse.getStatusLine();//获取http 状态线 int statusCode = statusLine.getStatusCode();//获取状态码 responseHeaders = convertHeaders(httpResponse.getAllHeaders()); // Handle cache validation.//处理缓存验证 if (statusCode == HttpStatus.SC_NOT_MODIFIED) {//返回缓存数据 return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry().data, responseHeaders, true); } //把HttpEntity转化为byte[]数据 responseContents = entityToBytes(httpResponse.getEntity()); // if the request is slow, log it.//如果请求很慢,就打印出来看一下 long requestLifetime = SystemClock.elapsedRealtime() - requestStart; logSlowRequests(requestLifetime, request, responseContents, statusLine);//打印 //连接正常但是返回无内容,抛出IO异常 if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT) { throw new IOException(); } return new NetworkResponse(statusCode, responseContents, responseHeaders, false); } catch (SocketTimeoutException e) {//读取超时,重试 attemptRetryOnException("socket", request, new TimeoutError()); } catch (ConnectTimeoutException e) {//连接超时,重试 attemptRetryOnException("connection", request, new TimeoutError()); } catch (MalformedURLException e) {//Bad URL throw new RuntimeException("Bad URL " + request.getUrl(), e); } catch (IOException e) {//IO异常 int statusCode = 0; NetworkResponse networkResponse = null; if (httpResponse != null) { statusCode = httpResponse.getStatusLine().getStatusCode(); } else {//如果没有返回httpResponse,就说明没连接 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts throw new NoConnectionError(e); } VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl()); if (responseContents != null) {//返回数据不为空 networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);//创建响应体 if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {//认证失败异常,重试 attemptRetryOnException("auth", request, new AuthFailureError(networkResponse)); } else {//服务器异常 // TODO: Only throw ServerError for 5xx status codes. throw new ServerError(networkResponse);//只有状态码为5XX才抛出服务器异常 } } else {//网络异常 throw new NetworkError(networkResponse); } } } } A:先是通过mHttpStack把请求执行并且获取它的响应结果,根据HttpStatus做出各种判断。 B:然后再把httpResponse的Entity转化为ByteArray,并处理各种发生的异常。 C:最后的过程是这样的:通过Volley创建一个RequestQueue请求队列,当这个队列开始运作的时候会启动NetworkDispatcher这个工作线程,而BasicNetwork的performRequest()的方法就在NetworkDispatcher线程run()方法中调用,然后通过mHttpStack的performRequest()方法获取一个networkResponse,在NetworkDispatcher线程把这个networkResponse转化为期望的数据类型,比如Response,Response,Response。 谷歌Volley网络框架讲解——第一篇 这个框架对网络请求数据缓存做了很多优化,其他说明就请自行百度吧。。。 在这梳理一下网络请求的过程: advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 先看一段网络请求的代码吧 mQueue = Volley.newRequestQueue(getApplicationContext()); //StringRequest四个构造参数分别是Request类型,url,网络请求响应监听器,错误监听器 mQueue.add(new StringRequest(Method.GET, ";, new Listener(){ @Override public void onResponse(String arg0) { // TODO Auto-generated method stub Log.e("onResponse", arg0); } }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub Log.e("onErrorResponse", arg0.toString()); } })); mQueue.start(); mQueue = Volley.newRequestQueue(getApplicationContext()); //StringRequest四个构造参数分别是Request类型,url,网络请求响应监听器,错误监听器 mQueue.add(new StringRequest(Method.GET, ";, new Listener(){ @Override public void onResponse(String arg0) { // TODO Auto-generated method stub Log.e("onResponse", arg0); } advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub Log.e("onErrorResponse", arg0.toString()); } })); mQueue.start(); Volley 首先,通过Volley的静态方法new一个请求队列,再调用请求队列(RequestQueue是由缓存和网络线程构造的)的start方法。 然后,得到一个RequestQueue请求队列时,就添加一个请求Request,Volley提供有三种请求的封装,一个是StringRequest,一个事ImageRequest,还有一个是JsonRequest。 最后,调用RequestQueue的start方法就可以开始一条网络请求了。 可以看出Volley只是RequestQueue的一个helper。 RequestQueue 所以,这里一个最核心的就是RequestQueue请求队列这个了。 这个请求队列是有两个东西完成,一个是cache一个network。 这个RequestQueue的构造形式有四种:除了上面这两个还有线程池数量,响应委派。 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 1,public RequestQueue(Cache cache, Network network, int threadPoolSize,ResponseDelivery delivery) 2, public RequestQueue(Cache cache, Network network, int threadPoolSize) 3,public RequestQueue(Cache cache, Network network) 其实Cache和Network真正的工作者还是CacheDispatcher缓存调度和NetworkDispatcher网络调 度。下期再详细讲解Dispatcher。。 RequestQueue的成员方法主要有下面几个: public void start();//请求队列开始进行调度发vgm public void stop();//队列退出调度 public Request add(Request request);//添加一个请求,通过调用start()来执行 void finish(Request request);//这个方法应该是释放请求资源的方法 public void cancelAll();//取消当前的请求 NetworkDispatcher 这个就是网络调度的核心,这是一个继承Thread的类,构造方法如下 public NetworkDispatcher(BlockingQueue queue,Network network, Cache cache,ResponseDelivery delivery) 一个Request的队列,Network ,Cache ,ResponseDelivery 。。。 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 它的run()方法就是把请求队列取出一个Request,让Network 去执行一次请求再通过ResponseDelivery 进行解析。 Network /** * An interface for performing requests. * 这是执行一个请求的接口 */ public interface Network { /** * Performs the specified request. * @param request Request to process * @return A {@link NetworkResponse} with data and caching metadata; will never be null * @throws VolleyError on errors */ public NetworkResponse performRequest(Request request) throws VolleyError; } 这是一个接口,执行请求获取响应,一个是NetworkResponse 一个是Request。 Request 它的构造方法: public Request(int method, String url, Response.ErrorListener listener) 第一个method就是请求类型,包括get,put,post,delete;第二个就是请求的url咯,第三个是ErrorListener。 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts subclass有StringRequest,ImageRequest,JsonObjectRequest,JsonArrayRequest。 1.StringRequest是继承Request ,对应String的ResponseListener交付请求结果。 public StringRequest(int method, String url, Listener listener,ErrorListener errorListener) 2.JsonRequest有JsonObjectRequest和JsonArrayRequest两个子类,通过 parseNetworkResponse(NetworkResponse response)来处理JsonObject和JsonArray两个不同 结果。 3.ImageRequest的构造参数比较多,主要针对图片的一些处理工作。多了一个限制图片最大长宽和 configOptions图片类的东西。 public ImageRequest(String url, Response.Listener listener, int maxWidth, int maxHeight,Config decodeConfig, Response.ErrorListener errorListener) Response Request有个 把NetworkResponse 解析成 Response 的抽象方法。 abstract protected Response parseNetworkResponse(NetworkResponse response); 然后调用Response.Listener的接口返回String,Json或bitmap。最后得到了想要的东西就可以随 便怎么处理了。 Response有两个接口,一个是Listener回调期望内容的接口和一个ErrorListener接口。 /** Callback interface for delivering parsed responses. */ public interface Listener { advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts /** Called when a response is received. */ public void onResponse(T response); } onResponse()把想要的json或者string,bitmap数据传回来 /** Callback interface for delivering error responses. */ public interface ErrorListener { /** * Callback method that an error has been occurred with the * provided error code and optional user-readable message. */ public void onErrorResponse(VolleyError error); } onErrorResponse会返回一个VolleyError 的错误,可以根据这些error进行响应的处理。 VolleyError 这个类继承Exception,用来捕获各宗异常。 在NetworkDispatcher里有个处理异常的方法parseAndDeliverNetworkErrorRequest request, VolleyError error(); 在请求的过程中把各种异常抛出,然后通过Response的ErrorListener捕获异常进行处理。 advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 整个过程差不多就是这样,梳理一番就很清楚,不过谷歌的代码就是很神,比其他网络上哪些炒来炒去的框架质量好不少,层次分明结构清晰,看着都赏心悦目。 我们再回过头来看一下这个请求方法的使用,就很清楚了。 mQueue = Volley.newRequestQueue(getApplicationContext()); //StringRequest四个构造参数分别是Request类型,url,网络请求响应监听器,错误监听器 mQueue.add(new StringRequest(Method.GET, ";, new Listener(){ @Override public void onResponse(String arg0) { // TODO Auto-generated method stub Log.e("onResponse", arg0); } }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub Log.e("onErrorResponse", arg0.toString()); } })); mQueue.start() advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts
本文档为【volley网络访问框架】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_597436
暂无简介~
格式:doc
大小:119KB
软件:Word
页数:0
分类:生活休闲
上传时间:2018-04-28
浏览量:16