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
 /**
  * 通过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.º 3
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;
 }