/** * get请求String */ private void volleyStringGet(){ Response.Listener<String> listener = new Response.Listener<String>() { @Override public void onResponse(String response) { //数据请求成功返回 Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); } }; StringRequest request = new StringRequest(Method.GET, url, listener, errorListener); request.setTag("jackStringRequest"); MyApplication.getHttpRequestQueue().add(request); }
private String volleyPostStringRequest() { StringRequest request = new StringRequest(Request.Method.POST, Constant.JUHE_URL_POST, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show(); } }){ /* getParams() 用户在Volley中使用post方式请求数据中参数的传递 */ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> hashMap = new HashMap<>(); hashMap.put("phone", "13429667914"); hashMap.put("key", "562609042fbd47baa063b1a2c4637758"); return hashMap; } }; request.setTag(STRING_POST_TAG); MyApplication.getHttpQueues().add(request); return request.getTag().toString(); }
mRequest.setTag(VOLLEY_TAG);
private String volleyGetStringRequest() { StringRequest request = new StringRequest(Request.Method.GET, Constant.JUHE_URL_GET, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show(); } }); request.setTag(STRING_GET_TAG); MyApplication.getHttpQueues().add(request); return request.getTag().toString(); } }
/** * Send a GET request. * * @param url The server url * @param params GET parameters * @param resListener Called when receiving response * @param errListener Called when error occurs */ public void get(String url, Map<String, String> params, Response.Listener<String> resListener, Response.ErrorListener errListener) { String getUrl = buildUrl(url, params); Log.d(TAG, "Get url: " + getUrl); StringRequest req = new StringRequest(getUrl, resListener, errListener); req.setRetryPolicy(new DefaultRetryPolicy(Config.REQ_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); req.setTag(REQUEST_TAG); queue.add(req); }
/** * post请求 */ private void volleyStringPost() { String url = "http://apis.juhe.cn/mobile/get?"; Response.Listener<String> listener = new Response.Listener<String>() { @Override public void onResponse(String response) { if(response != null) { Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show(); Log.d(TAG, "JSONObject-----------" + response.toString()); } } }; StringRequest request = new StringRequest(Method.POST, url, listener, errorListener){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> hashMap = new HashMap<String, String>(); hashMap.put("phone", "13811253688"); hashMap.put("key", "cf365ff9aa9f1daddea8357b43267fd7"); return hashMap; } }; request.setTag("jackPost"); MyApplication.getHttpRequestQueue().add(request); }
private void loadData() { StringRequest stringRequest = new StringRequest(Request.Method.GET, ZHIHU_API, new Response.Listener<String>() { @Override public void onResponse(String response) { dealWithResponse(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); //给stringRequest设置Tag,便于在Activity销毁时cancel掉网络请求 stringRequest.setTag(this.getLocalClassName()); BannerApplication.mInstance.getRequestQueue().add(stringRequest); }
private void testFileUpload() { RequestParams params = new RequestParams(); params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044d04fa1ea1442306755512"); params.addBodyParameter("hello_video", new File("/sdcard/rcWish/VideoCapture/20150914_153044.mp4")); params.addBodyParameter("hello_pic", new File("/sdcard/rcWish/VideoCapture/20150914_153044.jpg")); String url = "http://yourhost/index.php?act=ActorSetting&m=updateVideo"; StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e(TAG, response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }); request.setLoadingListener(new Response.LoadingListener() { @Override public void onLoading(boolean isUpload, long total, long current) { Log.e(TAG, "isUpload:" + isUpload + ",total:" + total + ",current:" + current); } }); request.setRequestParams(params); request.setTag(TAG); mRequestQueue.add(request); }
/** * Send a POST request. * * @param url The server url * @param params POST parameters * @param resListener Called when receiving response * @param errListener Called when error occurs */ public void post(String url, final Map<String, String> params, Response.Listener<String> resListener, Response.ErrorListener errListener) { StringRequest req = new StringRequest(Request.Method.POST, url, resListener, errListener) { @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } }; req.setRetryPolicy(new DefaultRetryPolicy(Config.REQ_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); req.setTag(REQUEST_TAG); queue.add(req); }
request.setTag(Constant.VOLLEY_TAG); //设置标签
request.setRetryPolicy(new DefaultRetryPolicy(mBuilder.timeOut, mBuilder.retry, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); request.setTag(mBuilder.tag); mQueue.add(request);
request.setRetryPolicy(new DefaultRetryPolicy(mBuilder.timeOut, mBuilder.retry, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); request.setTag(mBuilder.tag); mQueue.add(request);
public static void RequestGet(Context context, String url, String tag, VolleyInterface volleyInterface) { MyApplication.getHttpQueues().cancelAll(tag); // 先清空请求队列,防止出现重复的请求消耗内存 sStringRequest = new StringRequest(Request.Method.GET, url, volleyInterface.loadingListener(), volleyInterface.errorListener()); sStringRequest.setTag(tag); MyApplication.getHttpQueues().add(sStringRequest); MyApplication.getHttpQueues().start(); } public static void RequestPost(Context context, String url, String tag, Map<String, String> params, VolleyInterface volleyInterface) {
public static void RequestPost(Context context, String url, String tag, Map<String, String> params, VolleyInterface volleyInterface) { MyApplication.getHttpQueues().cancelAll(tag); sStringRequest = new StringRequest(url, volleyInterface.loadingListener(), volleyInterface.errorListener()){ @Override protected Map<String, String> getParams() throws AuthFailureError { return super.getParams(); } }; sStringRequest.setTag(tag); MyApplication.getHttpQueues().add(sStringRequest); MyApplication.getHttpQueues().start(); } }
/** * 执行get请求 * @param url * @param tag * @param listener */ public static void requestGet(String url, String tag, VolleyListener listener){ MyApplication.getHttpRequestQueue().cancelAll(tag); mStringRequest = new StringRequest(Request.Method.GET, url, listener.loadingListener(), listener.errorListener()); mStringRequest.setTag(tag); MyApplication.getHttpRequestQueue().add(mStringRequest); MyApplication.getHttpRequestQueue().start(); }
private void testNormalString() { RequestParams params = new RequestParams(); params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044d04fa1ea1442306755512"); params.addBodyParameter("birthday", "1986-01-01"); String url = "http://yourhost/index.php?act=User&m=updateMyProfile"; StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e(TAG, response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }); request.setRequestParams(params); request.setTag(TAG); mRequestQueue.add(request); }
/** * 执行post请求 * @param url * @param tag * @param params * @param listener */ public static void requestPost(String url, String tag, final Map<String, String> params, VolleyListener listener){ MyApplication.getHttpRequestQueue().cancelAll(tag); mStringRequest = new StringRequest(Request.Method.POST, url, listener.loadingListener(), listener.errorListener()){ @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } }; mStringRequest.setTag(tag); MyApplication.getHttpRequestQueue().add(mStringRequest); MyApplication.getHttpRequestQueue().start(); }