【网络通信:Volley】请求的发送与响应之String
2015-09-14 15:58
555 查看
作者:郭孝星
微博:郭孝星的新浪微博
邮箱:allenwells@163.com
博客:http://blog.csdn.net/allenwells
Github:https://github.com/AllenWells
下面我们举一个例子来简单的演示一下Volley的请求与响应流程。
举例
通过Volley发送一条简单的请求。
1 添加INTERNET权限
首先在AndroidManifest.xml添加权限
2 创建RequestQueue对象
3 创建StringRequest对象,设置响应成功监听器和响应失败监听器
4 将StringRequest添加到newRequestQueue里
完整的代码如下所示,这是一个在Fragment中向http://www.baidu.com请求数据并把数据显示到TextView中的一个Demo。
通过上述代码我们可以发现整个过程可以直接运行在UI线程中,这是因为Volley内部已经帮我们处理好了线程问题,直接运行在UI线程中可以鞥更好的帮助我们请求和填充UI数据。
代码执行成功后,我们可以在TextView中发现http://www.baidu.com返回的数据,如下所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/07/c37a398ca8b11641924d724ade23dd14)
这里在提一下如何取消请求。取消请求是需要两步即可:
定义一个TAG并把它添加到请求中
在需要取消请求的地方调用mRequestQueue.cancelAll(TAG)取消所有被TAG标记的Request。
上述流程的最后一步是 requestQueue.add(stringRequest),在add以后Volley内部经历了哪些过程昵?
我们来看一下,当我们构建一个请求并把它添加到RequestQueueR中以后,Volley会首先运行一个缓存处理线程和网络调度线程池,添加进来的的StringRequestS会被缓存处理线程拾起,如果请求可以从缓存处理线程中请求到服务,那么缓存的响应就会在缓存处理线程中被解析并分发到主线程中。如果不能从缓存处理线程中请求到服务,那么请求会被放置到网络队列中,第一个可用的网络线程从网络队列中取出请求,执行Http事务,解析工作线程中的响应,响应解析完成后,将其写入缓存中并随后传递到主线程中,整个响应的生命周期如下图所示:
微博:郭孝星的新浪微博
邮箱:allenwells@163.com
博客:http://blog.csdn.net/allenwells
Github:https://github.com/AllenWells
下面我们举一个例子来简单的演示一下Volley的请求与响应流程。
举例
通过Volley发送一条简单的请求。
1 添加INTERNET权限
首先在AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.INTERNET"/>
2 创建RequestQueue对象
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
3 创建StringRequest对象,设置响应成功监听器和响应失败监听器
StringRequest stringRequest = new StringRequest(Request.Method.GET, requsetUrl, new Response.Listener<String>() { @Override public void onResponse(String s) { requestTV.setText( "The response from www.baidu.com is : " + s.substring(0, 500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { requestTV.setText("That is not response"); } });
4 将StringRequest添加到newRequestQueue里
requestQueue.add(stringRequest);
完整的代码如下所示,这是一个在Fragment中向http://www.baidu.com请求数据并把数据显示到TextView中的一个Demo。
package com.allenwells.onlineclient.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.allenwells.onlineclient.R;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
/**
* Created by allen on 2015/9/10.
*/
public class GoodsCategoryFragment extends Fragment {
private TextView requestTV = null;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View fragmentGoodscategoryView = inflater.inflate(R.layout.fragment_goodscategory, null);
requestTV = (TextView) fragmentGoodscategoryView.findViewById(R.id.request_text);
String requsetUrl = "http://www.baidu.com";
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
StringRequest stringRequest = new StringRequest(Request.Method.GET, requsetUrl, new Response.Listener<String>() { @Override public void onResponse(String s) { requestTV.setText( "The response from www.baidu.com is : " + s.substring(0, 500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { requestTV.setText("That is not response"); } });
requestQueue.add(stringRequest);
return fragmentGoodscategoryView;
}
}
通过上述代码我们可以发现整个过程可以直接运行在UI线程中,这是因为Volley内部已经帮我们处理好了线程问题,直接运行在UI线程中可以鞥更好的帮助我们请求和填充UI数据。
代码执行成功后,我们可以在TextView中发现http://www.baidu.com返回的数据,如下所示:
这里在提一下如何取消请求。取消请求是需要两步即可:
定义一个TAG并把它添加到请求中
在需要取消请求的地方调用mRequestQueue.cancelAll(TAG)取消所有被TAG标记的Request。
Define your tag and add it to your requests. public static final String TAG = "MyTag"; StringRequest stringRequest; // Assume this exists. RequestQueue mRequestQueue; // Assume this exists. // Set the tag on the request. stringRequest.setTag(TAG); // Add the request to the RequestQueue. mRequestQueue.add(stringRequest); In your activity's onStop() method, cancel all requests that have this tag. @Override protected void onStop () { super.onStop(); if (mRequestQueue != null) { mRequestQueue.cancelAll(TAG); } }
上述流程的最后一步是 requestQueue.add(stringRequest),在add以后Volley内部经历了哪些过程昵?
我们来看一下,当我们构建一个请求并把它添加到RequestQueueR中以后,Volley会首先运行一个缓存处理线程和网络调度线程池,添加进来的的StringRequestS会被缓存处理线程拾起,如果请求可以从缓存处理线程中请求到服务,那么缓存的响应就会在缓存处理线程中被解析并分发到主线程中。如果不能从缓存处理线程中请求到服务,那么请求会被放置到网络队列中,第一个可用的网络线程从网络队列中取出请求,执行Http事务,解析工作线程中的响应,响应解析完成后,将其写入缓存中并随后传递到主线程中,整个响应的生命周期如下图所示:
相关文章推荐
- 计算机网络--NAT
- HTTPS与SSL详解(二)
- HTTPS与SSL详解(一)
- 理解strong与weak)强引用与弱引用转载自《http://blog.sina.com.cn/s/blog_7c8dc2d50101lc08.html》
- JAVA: httpclient 详细说明——第四章;
- 一文读懂卷积神经网络
- Nginx Http 核心模块中Server Location 配置
- VM虚拟机与宿主机网络互联的方法
- 【网络通信:Volley】Volley开篇
- Linux系统下修改网络配置
- Android 网络通信框架Volley
- Win10微软小娜无法连接网络怎么办?微软小娜无法连接网络问题解决方法
- Linux 下curl模拟Http 的get or post请求。
- HttpClient
- 我的openwrt学习笔记(三十):webserver之uhttpd
- LwIP网络接口结构体---netif
- Android使用VideoView播放网络视频
- tcp/ip详解学习(一)
- Android之HttpClient 和HttpResponse 小结
- HTTP协议详解