volley已经出来一年多了,目前的市场基本和android async http持平,下面我给出volley的简单用法
首先在application中创建一个公共的请求队列:
package com.example.xuan.netvolleydemo; import android.app.Application; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; public class MyApplication extends Application { public static RequestQueue requestQueue; public static RequestQueue getRequestQueue() { return requestQueue; } @Override public void onCreate() { super.onCreate(); requestQueue= Volley.newRequestQueue(getApplicationContext()); } }
1:GET请求
private void volley_get() { String url = "http://apis.juhe.cn/mobile/get?phone=15001099630&key=335adcc4e891ba4e4be6d7534fd54c5d"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { Toast.makeText(context, "res:" + s, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(context, "error:" + volleyError, Toast.LENGTH_SHORT).show(); } }); request.setTag(get_tag); MyApplication.getRequestQueue().add(request); }
2:POST请求
private void volley_post() { String url = "http://apis.juhe.cn/mobile/get?"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { Toast.makeText(context, "res:" + s, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(context, "error:" + volleyError, Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("phone", "15001099630"); params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); //return super.getParams(); return params; } }; request.setTag(post_tag); MyApplication.getRequestQueue().add(request); }
package com.example.xuan.netvolleydemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import java.util.HashMap; import java.util.Map; public class MainActivity extends Activity { private static String get_tag = "getTag"; private static String post_tag = "postTag"; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; setContentView(R.layout.activity_main); // volley_get(); volley_post(); } private void volley_get() { String url = "http://apis.juhe.cn/mobile/get?phone=15001099630&key=335adcc4e891ba4e4be6d7534fd54c5d"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { Toast.makeText(context, "res:" + s, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(context, "error:" + volleyError, Toast.LENGTH_SHORT).show(); } }); request.setTag(get_tag); MyApplication.getRequestQueue().add(request); } private void volley_post() { String url = "http://apis.juhe.cn/mobile/get?"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { Toast.makeText(context, "res:" + s, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(context, "error:" + volleyError, Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("phone", "15001099630"); params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); //return super.getParams(); return params; } }; request.setTag(post_tag); MyApplication.getRequestQueue().add(request); } @Override protected void onStop() { super.onStop(); MyApplication.getRequestQueue().cancelAll(get_tag); MyApplication.getRequestQueue().cancelAll(post_tag); } }