Ejemplo n.º 1
0
 /**
  * 内部调用接口,发起一个通用的GsonRequest请求
  *
  * @param url 请求地址
  * @param clazz 响应的模型
  * @param success 成功的回调
  * @param error 失败的回调
  * @param name 要上传文件的字段名(可选)
  * @param file 要上传的文件(可选)
  * @param sendImmediately 是否创建请求后马上发送,如果传false,则需要自己将request加入到requestQueue中去
  * @return 返回的Request对象可以用于取消请求
  */
 public static <T> Request request(
     int method,
     String url,
     String refer,
     String name,
     File file,
     Class<T> clazz,
     Response.Listener<T> success,
     Response.ErrorListener error,
     boolean sendImmediately) {
   Request request;
   if (null == file) {
     request = new GsonRequest<T>(method, url, refer, clazz, null, null, success, error);
     request.setRetryPolicy(
         RetryPolicyFactory.getRetryPolicy(RetryPolicyFactory.RETRY_POLICY.NORMAL));
   } else {
     request = new GsonRequest<T>(method, url, refer, clazz, name, file, success, error);
     request.setRetryPolicy(
         RetryPolicyFactory.getRetryPolicy(RetryPolicyFactory.RETRY_POLICY.MULTIPART));
   }
   if (sendImmediately) {
     requestQueue.add(request);
   }
   return request;
 }
Ejemplo n.º 2
0
 @Test
 public void otherClientError() throws Exception {
   for (int i = 400; i <= 499; i++) {
     if (i == 401 || i == 403) {
       // covered above.
       continue;
     }
     MockHttpStack mockHttpStack = new MockHttpStack();
     BasicHttpResponse fakeResponse =
         new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
     mockHttpStack.setResponseToReturn(fakeResponse);
     BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
     Request<String> request = buildRequest();
     request.setRetryPolicy(mMockRetryPolicy);
     doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
     try {
       httpNetwork.performRequest(request);
     } catch (VolleyError e) {
       // expected
     }
     // should not retry other 400 errors.
     verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
     reset(mMockRetryPolicy);
   }
 }
Ejemplo n.º 3
0
  protected void modifyJiage() {

    String PriceHour, PriceMonth;

    if (MyTextUtils.isEmpty(etPriceHour.getText().toString())) {
      ToastUtils.showSuperToastComment(getApplicationContext(), "时租价格不能为空");
      return;
    } else {
      PriceHour = etPriceHour.getText().toString();
    }

    if (MyTextUtils.isEmpty(etPriceMonth.getText().toString())) {
      ToastUtils.showSuperToastComment(getApplicationContext(), "月租价格不能为空");
      return;
    } else {
      PriceMonth = etPriceMonth.getText().toString();
    }

    String url;

    url = PathConfig.ADDRESS + "/base/breleasepark/setPrice";
    url = MyTextUtils.urlPlusFoot(url);

    Map<String, String> params = new HashMap<String, String>();
    params.put("Code", detailMap.get("CODE"));
    params.put("PriceHour", PriceHour);
    params.put("PriceMonth", PriceMonth);

    Request<JSONObject> request =
        new VolleyCommonPost(
            url,
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                String jsondata = response.toString();
                Map<String, String> message =
                    JSON.parseObject(jsondata, new TypeReference<Map<String, String>>() {});

                if (message.get("status").equals("true")) {
                  dismissDialog();
                  finish();
                  ToastUtils.showSuperToastAlertGreen(ModifyParkJiaGe.this, message.get("info"));
                } else {
                  ToastUtils.showSuperToastAlert(ModifyParkJiaGe.this, message.get("info"));
                  finish();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                dismissDialog();
                ToastUtils.showSuperToastAlert(ModifyParkJiaGe.this, "连接服务器失败,请稍后重试!");
              }
            },
            params);
    request.setRetryPolicy(new DefaultRetryPolicy(200 * 1000, 1, 1.0f));
    ApplicationController.getInstance().addToRequestQueue(request);
  }
Ejemplo n.º 4
0
  /**
   * Creates a new request with the given method (one of the values from {@link
   * com.android.volley.Request.Method}), URL, and error listener. Note that the normal response
   * listener is not provided here as delivery of responses is provided by subclasses, who have a
   * better idea of how to deliver an already-parsed response.
   */
  public Request(int method, String url, Response.ErrorListener listener) {
    mMethod = method;
    mUrl = url;
    mErrorListener = listener;
    setRetryPolicy(new DefaultRetryPolicy());

    mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0 : Uri.parse(url).getHost().hashCode();
  }
Ejemplo n.º 5
0
 /**
  * 通过GET方法下载一个文件,内部会走HTTP的缓存机制
  *
  * @param url
  * @param success
  * @param error
  * @return
  */
 public static Request<File> download(
     String url, Response.Listener<File> success, Response.ErrorListener error) {
   Request<File> request = new FileRequest(Method.GET, url, success, error);
   request.setRetryPolicy(
       RetryPolicyFactory.getRetryPolicy(RetryPolicyFactory.RETRY_POLICY.DOWNLOAD));
   requestQueue.add(request);
   return request;
 }
Ejemplo n.º 6
0
 /**
  * 发起一个同步HTTP请求
  *
  * @param method
  * @param url
  * @param name
  * @param file
  * @param clazz
  * @return
  * @throws java.util.concurrent.ExecutionException
  * @throws InterruptedException
  */
 public static <T> T requestSync(
     int method, String url, String refer, String name, File file, Class<T> clazz)
     throws ExecutionException, InterruptedException {
   Request<T> request;
   RequestFuture<T> future = RequestFuture.newFuture();
   if (null == file) {
     request = new GsonRequest<T>(method, url, refer, clazz, null, null, future, future);
     request.setRetryPolicy(
         RetryPolicyFactory.getRetryPolicy(RetryPolicyFactory.RETRY_POLICY.NORMAL));
   } else {
     request = new GsonRequest<T>(method, url, refer, clazz, name, file, future, future);
     request.setRetryPolicy(
         RetryPolicyFactory.getRetryPolicy(RetryPolicyFactory.RETRY_POLICY.MULTIPART));
   }
   requestQueue.add(request);
   T t = future.get();
   return t;
 }
 public <T> void addToRequestQueue(Request<T> req) {
   req.setTag(TAG);
   req.setRetryPolicy(
       new DefaultRetryPolicy(
           DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
           DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
           DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
   getRequestQueue().add(req);
 }
Ejemplo n.º 8
0
 public void addToQueue(Request request) {
   if (request != null) {
     request.setTag(this);
     if (fRequestQueue == null) fRequestQueue = volley.getRequestQueue();
     request.setRetryPolicy(
         new DefaultRetryPolicy(60000, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
     onPreStartConnection();
     fRequestQueue.add(request);
   }
 }
Ejemplo n.º 9
0
 private static void dispatchToQueue(Request pRequest, Context pContext) {
   if (!isNetworkConnected(pContext) && pRequest.getMethod() != Request.Method.GET) {
     // non-GET request should not return cached response in case of no network
     pRequest.deliverError(new VolleyError(RESPONSE_NO_INTERNET));
     return;
   }
   switch (pRequest.getMethod()) {
     case Request.Method.POST:
       {
         pRequest.setRetryPolicy(RETRY_POLICY_POST);
         break;
       }
     case Request.Method.GET:
     default:
       {
         pRequest.setRetryPolicy(RETRY_POLICY_GET);
         break;
       }
   }
   mRequestQueue.add(pRequest);
 }
Ejemplo n.º 10
0
 public static <T> void addRequest(RequestQueue requestQueue, Request<T> request, Object tag) {
   if (tag != null) {
     request.setTag(tag);
   }
   request.setShouldCache(false);
   request.setRetryPolicy(
       new DefaultRetryPolicy(
           TIME_OUT,
           DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
           DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
   requestQueue.add(request);
 }
Ejemplo n.º 11
0
 @Test
 public void connectTimeout() throws Exception {
   MockHttpStack mockHttpStack = new MockHttpStack();
   mockHttpStack.setExceptionToThrow(new ConnectTimeoutException());
   BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
   Request<String> request = buildRequest();
   request.setRetryPolicy(mMockRetryPolicy);
   doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
   try {
     httpNetwork.performRequest(request);
   } catch (VolleyError e) {
     // expected
   }
   // should retry connection timeouts
   verify(mMockRetryPolicy).retry(any(TimeoutError.class));
 }
Ejemplo n.º 12
0
 @Test
 public void forbidden() throws Exception {
   MockHttpStack mockHttpStack = new MockHttpStack();
   BasicHttpResponse fakeResponse =
       new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 403, "Forbidden");
   mockHttpStack.setResponseToReturn(fakeResponse);
   BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
   Request<String> request = buildRequest();
   request.setRetryPolicy(mMockRetryPolicy);
   doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
   try {
     httpNetwork.performRequest(request);
   } catch (VolleyError e) {
     // expected
   }
   // should retry in case it's an auth failure.
   verify(mMockRetryPolicy).retry(any(AuthFailureError.class));
 }
Ejemplo n.º 13
0
 @Test
 public void serverError_disableRetries() throws Exception {
   for (int i = 500; i <= 599; i++) {
     MockHttpStack mockHttpStack = new MockHttpStack();
     BasicHttpResponse fakeResponse =
         new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
     mockHttpStack.setResponseToReturn(fakeResponse);
     BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
     Request<String> request = buildRequest();
     request.setRetryPolicy(mMockRetryPolicy);
     doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
     try {
       httpNetwork.performRequest(request);
     } catch (VolleyError e) {
       // expected
     }
     // should not retry any 500 error w/ HTTP 500 retries turned off (the default).
     verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
     reset(mMockRetryPolicy);
   }
 }
Ejemplo n.º 14
0
 @Test
 public void serverError_enableRetries() throws Exception {
   for (int i = 500; i <= 599; i++) {
     MockHttpStack mockHttpStack = new MockHttpStack();
     BasicHttpResponse fakeResponse =
         new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
     mockHttpStack.setResponseToReturn(fakeResponse);
     BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
     Request<String> request = buildRequest();
     request.setRetryPolicy(mMockRetryPolicy);
     request.setShouldRetryServerErrors(true);
     doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
     try {
       httpNetwork.performRequest(request);
     } catch (VolleyError e) {
       // expected
     }
     // should retry all 500 errors
     verify(mMockRetryPolicy).retry(any(ServerError.class));
     reset(mMockRetryPolicy);
   }
 }
Ejemplo n.º 15
0
 public <T> void addToRequestQueue(Request<T> req) {
   req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
   mRequestQueue.add(req);
 }