为什么会有Volley
话说android的网络连接基本可以使用Java当中的网络连接,已经可以使用HttpClient,HttpURLConnection,在2013年谷歌推出volley,其实就是为了更方便简单的网络连接
Volley提供的功能
简单的讲,提供了如下主要的功能:
1、封装了的异步的RESTful 请求API;
2、一个优雅和稳健的请求队列;
3、一个可扩展的架构,它使开发人员能够实现自定义的请求和响应处理机制;
4、能够使用外部HTTP Client库;
5、缓存策略;
6、自定义的网络图像加载视图(NetworkImageView,ImageLoader等);
简单使用volley
【1】导入Volley包
【2】RequestQueue queue = Volley.newRequestQueue(this);
【3】
StringRequest request = new StringRequest(Request.Method.GET, "http://www.360.com", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// mWebView.loadUrl("http://www.360.com");
// mWebView.loadData(response,"text/html","utf-8");//此方法会使中文乱码。使用下面一条
//mWebView.loadDataWithBaseURL("http://www.360.com",response,"text/html","utf-8",null);
mWebView.loadDataWithBaseURL(null, response, "text/html", "utf-8", null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("网络连接错误");
}
});
【4】queue.add(request);
下面介绍使用Volley获取网络图片以及GET和POST方法
效果如下
GET
private void VolleyDoGet() {
// RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, "http://www.360.com", new Response.Listener<String>() {
//连接成功返回response
@Override
public void onResponse(String response) {
// mWebView.loadUrl("http://www.360.com");
// mWebView.loadData(response,"text/html","utf-8");//此方法会使中文乱码。使用下面一条
//mWebView.loadDataWithBaseURL("http://www.360.com",response,"text/html","utf-8",null);
mWebView.loadDataWithBaseURL(null, response, "text/html", "utf-8", null);
}
}, new Response.ErrorListener() {
//如果连接错误自定义页面
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("网络连接错误");
}
});
// queue.add(request);
MySingleton.getInstance(this).addToRequestQueue(request);
}
###POST
private void VolleyDoPost() {
// RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.0.30:8080/MyWebTest/MyTestServerlet", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// mWebView.loadUrl("http://www.360.com");
// mWebView.loadData(response,"text/html","utf-8");//此方法会使中文乱码。使用下面一条
//mWebView.loadDataWithBaseURL("http://www.360.com",response,"text/html","utf-8",null);
mWebView.loadDataWithBaseURL(null, response, "text/html", "utf-8", null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("网络连接错误");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap map = new HashMap();
map.put("username", "lisi");
return map;
}
};
// queue.add(request);
MySingleton.getInstance(this).addToRequestQueue(request);
}
这里通过自己设置的服务器,可以向服务器发送消息,比如注册信息
下面是完整代码,包括一个VolleyActivity 和单例设计模式
public class VolleyActivity extends Activity implements View.OnClickListener {
private Button mButton_volley_post;
private Button mButton_volley_get;
private TextView mTextView;
private WebView mWebView;
private Button mButton_getphoto;
private NetworkImageView networkImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley);
mButton_volley_post = (Button) findViewById(R.id.button_volley_post);
mButton_volley_get= (Button) findViewById(R.id.button_volley_get);
mButton_volley_get.setOnClickListener(this);
mTextView = (TextView) findViewById(R.id.textview);
mButton_volley_post.setOnClickListener(this);
mWebView = (WebView) findViewById(R.id.webview);
mButton_getphoto = (Button) findViewById(R.id.button_getphoto);
mButton_getphoto.setOnClickListener(this);
networkImageView = (NetworkImageView) findViewById(R.id.netimage);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_getphoto:
LoadWebPhoto();
break;
case R.id.button_volley_post:
VolleyDoPost();
break;
case R.id.button_volley_get:
VolleyDoGet();
break;
default:
break;
}
}
private void LoadWebPhoto() {
networkImageView.setImageUrl("http://www.daxueit.com/upload/201408/13/101047281001237.png", MySingleton.getInstance(this).getImageLoader());
}
private void VolleyDoGet() {
// RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, "http://www.360.com", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// mWebView.loadUrl("http://www.360.com");
// mWebView.loadData(response,"text/html","utf-8");//此方法会使中文乱码。使用下面一条
//mWebView.loadDataWithBaseURL("http://www.360.com",response,"text/html","utf-8",null);
mWebView.loadDataWithBaseURL(null, response, "text/html", "utf-8", null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("网络连接错误");
}
});
// queue.add(request);
MySingleton.getInstance(this).addToRequestQueue(request);
}
private void VolleyDoPost() {
// RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.0.30:8080/MyWebTest/MyTestServerlet", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// mWebView.loadUrl("http://www.360.com");
// mWebView.loadData(response,"text/html","utf-8");//此方法会使中文乱码。使用下面一条
//mWebView.loadDataWithBaseURL("http://www.360.com",response,"text/html","utf-8",null);
mWebView.loadDataWithBaseURL(null, response, "text/html", "utf-8", null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("网络连接错误");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap map = new HashMap();
map.put("username", "lisi");
return map;
}
};
// queue.add(request);
MySingleton.getInstance(this).addToRequestQueue(request);
}
}
单例设计模式
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
//私有的构造器
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
//顺便生成MySingleton时候生成ImageLoader
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
//公共的获取实例的方法
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
/**
* 添加Request到Quene
* @param request
*/
public void addToRequestQueue(Request request) {
getRequestQueue().add(request);
}
/**
*
* @returnmImageLoader
*/
public ImageLoader getImageLoader() {
return mImageLoader;
}
}