Volley请求网络简单学习
2016-07-16 13:12
344 查看
请求网络的4种方式.get和post分别2种
自定义Application类
自定义封装接口 请求成功 和请求失败的回调
Volley的 简单二次封装
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * get 的2种请求方式 */ // GET_StringRequest(); // GET_JsonObjectRequest(); /** * post 的2种请求方式 */ // POST_StringRequest(); // POST_JsonObjectRequest(); /** * 自定义封装的 get请求 */ Volley_Get(); } /** * 初始化控件 */ private void initView() { iv_img = (ImageView) findViewById(R.id.iv_img); } /** * 自定义的 get请求 * 好处 : 可以在请求成功之前 全局的设置 加载框 不用再每个请求之前写 */ private void Volley_Get() { String url = "http://apis.juhe.cn/mobile/get?phone=18300077682&key=335adcc4e891ba4e4be6d7534fd54c5d"; VolleyRequest.RequestGet(this, url, "abcGet", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(String result) { Toast.makeText(MainActivity.this, "请求成功 : " + result, Toast.LENGTH_SHORT).show(); } @Override public void onMyError(VolleyError error) { Toast.makeText(MainActivity.this, "请求失败 : " + error, Toast.LENGTH_SHORT).show(); } }); } private void POST_StringRequest() { // String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?"; String url = "http://apis.juhe.cn/mobile/get?"; StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { //TODO 可以 全局的设置 弹出 加载中 Toast.makeText(MainActivity.this, "请求成功 : " + response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "请求失败 : " + error, Toast.LENGTH_SHORT).show(); //TODO 此处可以 提示请求失败 } }) { @Override //volley 在 post 请求时使用getParams()方法传递参数 protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> stringMap = new HashMap<>(); stringMap.put("phone", "18300077682"); stringMap.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); return stringMap; } }; stringRequest.setTag("Tag_post"); MyApplication.getHttpQueues().add(stringRequest); } private void GET_JsonObjectRequest() { String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18300677682"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.GET, url, null, //get请求可以传null,但是 post请求是 参数的拼接 new Response.Listener<JSONObject>() { @Override //请求成功时 回调 public void onResponse(JSONObject response) { Toast.makeText(MainActivity.this, "请求成功 : " + response, Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "请求成功 : " + response.toString()); } }, new Response.ErrorListener() { @Override //请求失败时回调 public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "请求失败 : " + error, Toast.LENGTH_SHORT).show(); } }); jsonObjectRequest.setTag("Tag_GET"); //设置标签 MyApplication.getHttpQueues().add(jsonObjectRequest); //加入到全局队列中 } private void POST_JsonObjectRequest() { String url = "http://apis.juhe.cn/mobile/get?"; //String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?"; HashMap<String, String> stringMap = new HashMap<>(); stringMap.put("phone", "18300077682"); stringMap.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); // stringMap.put("apikey","b63a29ab2f4760818e4ca641cd355934"); JSONObject jsonObject = new JSONObject(stringMap); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Toast.makeText(MainActivity.this, "请求成功 : " + response.toString(), Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "请求成功 : " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "请求失败 : " + error, Toast.LENGTH_SHORT).show(); } }); jsonObjectRequest.setTag("Tag_GET"); //设置标签 MyApplication.getHttpQueues().add(jsonObjectRequest); //加入到全局队列中 } private void GET_StringRequest() { //String url = ""; //请求接口地址 //示例地址 请求接口地址 String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18300677682"; StringRequest request = new StringRequest( Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, "请求成功 : " + response, Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "请求成功 : " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "请求失败 : " + error, Toast.LENGTH_SHORT).show(); } }); request.setTag("Tag_GET"); //设置标签 MyApplication.getHttpQueues().add(request); //加入到全局队列中 } @Override protected void onStop() { super.onStop(); MyApplication.getHttpQueues().cancelAll("Tag_GET"); //此行代表Activity与 Volley 的生命周期进行关联 } }
自定义Application类
/** * 自定义Application类 * * 创建RequestQueue 请求队列对象,并对外暴露方法 */ public class MyApplication extends Application { public static RequestQueue sQueue; //请求队列 @Override public void onCreate() { super.onCreate(); sQueue = Volley.newRequestQueue(getApplicationContext()); //建立全局请求队列 } //获取请求队列 public static RequestQueue getHttpQueues(){ return sQueue; } }
自定义封装接口 请求成功 和请求失败的回调
** * 自定义封装 请求成功 和请求失败的回调 */ public abstract class VolleyInterface { public Context mContext; public static Response.Listener<String> mListener; public static Response.ErrorListener mErrorListener; public VolleyInterface(Context context, Response.Listener<String> listener, Response.ErrorListener errorListener) { this.mContext = context; this.mListener = listener; this.mErrorListener = errorListener; } /** * 自定义的 请求成功的回调方法 * * @return mListener */ public Response.Listener<String> loadingListener() { mListener = new Response.Listener<String>() { @Override public void onResponse(String response) { onMySuccess(response); } }; return mListener; } /** * 自定义的 请求失败的回调 * * @return */ public Response.ErrorListener errorListener() { mErrorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { onMyError(error); } }; return mErrorListener; } public abstract void onMySuccess(String result); public abstract void onMyError(VolleyError error); }
Volley的 简单二次封装
package com.net.volleydemo.volley; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.toolbox.StringRequest; import com.net.volleydemo.MyApplication; import java.util.Map; /** * Volley的 简单二次封装 */ public class VolleyRequest { public static StringRequest stringRequest; public static Context context; /** * 自定义的 get请求方法 * @param context 上下文 * @param url 请求地址 * @param tag 设置的标签 * @param vif 监听的回调方法 */ public static void RequestGet(Context context,String url,String tag,VolleyInterface vif) { MyApplication.getHttpQueues().cancelAll(tag); //在请求网络之前,把之前的请求全部取消,防止重复请求 stringRequest = new StringRequest(Request.Method.GET,url,vif.loadingListener(),vif.errorListener()); stringRequest.setTag(tag); MyApplication.getHttpQueues().add(stringRequest); MyApplication.getHttpQueues().start(); } /** * 自定义的 post请求方法 * @param context * @param url * @param tag * @param params 因为post的请求方法 所以要在 getParams() 中传递参数 * @param vif 请求成功或失败的监听 */ public static void RequestPost(Context context,String url,String tag, final Map<String,String> params,VolleyInterface vif) { MyApplication.getHttpQueues().cancelAll(tag); //在请求网络之前,把之前的请求全部取消,防止重复请求 stringRequest = new StringRequest(Request.Method.POST,url,vif.loadingListener(),vif.errorListener()){ @Override //因为是 post 请求 所以需要传递参数 protected Map<String, String> getParams() throws AuthFailureError { return params; } }; stringRequest.setTag(tag); //设置标签 MyApplication.getHttpQueues().add(stringRequest); //加入全局队列 MyApplication.getHttpQueues().start(); } }