Exemplo n.º 1
0
 public KoalaDownloadTask build() {
   Map<String, String> params = new HashMap<String, String>();
   Map<String, String> headers = new HashMap<String, String>();
   Request.Builder builder = new Request.Builder();
   if (task.req != null) {
     KoalaHttpType type = task.req.getClass().getAnnotation(KoalaHttpType.class);
     if (type != null) {
       KoalaHttpUtils.getParams(task.req, task.req.getClass(), params, headers);
       if (headers.size() > 0) {
         Set<Map.Entry<String, String>> set = params.entrySet();
         Iterator<Map.Entry<String, String>> iterator = set.iterator();
         while (iterator.hasNext()) {
           Map.Entry<String, String> maEntry = iterator.next();
           builder.addHeader(maEntry.getKey(), maEntry.getValue());
         }
       }
       if (params.size() > 0) {
         if (type.value() == KoalaRequestType.GET) {
           task.url += ("?" + KoalaHttpUtils.getNameValuePair(params));
         } else if (type.value() == KoalaRequestType.POST) {
           Set<Map.Entry<String, String>> set = params.entrySet();
           Iterator<Map.Entry<String, String>> iterator = set.iterator();
           FormEncodingBuilder bodyBuilder = new FormEncodingBuilder();
           while (iterator.hasNext()) {
             Map.Entry<String, String> maEntry = iterator.next();
             bodyBuilder.add(maEntry.getKey(), maEntry.getValue());
           }
           builder.post(bodyBuilder.build());
         }
       }
     }
     task.url = KoalaHttpUtils.buildUrl(task.url, task.req);
     KoalaRequestTag tag = task.req.getClass().getAnnotation(KoalaRequestTag.class);
     if (tag != null) {
       if (TextUtils.isEmpty(tag.value())) {
         builder.tag(SecurityMD5.ToMD5(task.url));
       } else {
         builder.tag(tag);
       }
     } else {
       builder.tag(SecurityMD5.ToMD5(task.url));
     }
   } else {
     builder.tag(SecurityMD5.ToMD5(task.url));
   }
   builder.url(task.url);
   task.request = builder.build();
   task.tag = task.request.tag();
   return task;
 }
Exemplo n.º 2
0
  private static Request transformRequest(HttpRequest request, Object tag) {
    Request.Builder builder = new Request.Builder();

    RequestLine requestLine = request.getRequestLine();
    String method = requestLine.getMethod();
    builder.url(requestLine.getUri());

    String contentType = null;
    for (Header header : request.getAllHeaders()) {
      String name = header.getName();
      if ("Content-Type".equals(name)) {
        contentType = header.getValue();
      } else {
        builder.header(name, header.getValue());
      }
    }

    RequestBody body = null;
    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
      if (entity != null) {
        // Wrap the entity in a custom Body which takes care of the content, length, and type.
        body = new HttpEntityBody(entity, contentType);

        Header encoding = entity.getContentEncoding();
        if (encoding != null) {
          builder.header(encoding.getName(), encoding.getValue());
        }
      }
    }
    builder.method(method, body);
    builder.tag(tag);

    return builder.build();
  }
Exemplo n.º 3
0
  public Response send(final Request.Builder requestBuilder, StringMap headers)
      throws QiniuException {
    if (headers != null) {
      headers.forEach(
          new StringMap.Consumer() {
            @Override
            public void accept(String key, Object value) {
              requestBuilder.header(key, value.toString());
            }
          });
    }

    requestBuilder.header("User-Agent", userAgent());
    long start = System.currentTimeMillis();
    com.squareup.okhttp.Response res = null;
    Response r;
    double duration = (System.currentTimeMillis() - start) / 1000.0;
    IpTag tag = new IpTag();
    try {
      res = httpClient.newCall(requestBuilder.tag(tag).build()).execute();
    } catch (IOException e) {
      e.printStackTrace();
      throw new QiniuException(e);
    }
    r = Response.create(res, tag.ip, duration);
    if (r.statusCode >= 300) {
      throw new QiniuException(r);
    }

    return r;
  }
 /**
  * 构造GET请求对象
  *
  * @param url
  * @param params
  * @return
  */
 private Request buildGetRequest(String url, Map<String, Object> params) {
   HttpUrl fullUrl = buildUrl(url, params);
   Request.Builder builder = new Request.Builder().url(fullUrl);
   wrapHttpHeader(builder, url);
   builder.tag(url);
   return builder.build();
 }
Exemplo n.º 5
0
  @Override
  public Call getDetail(String videoId, Callback callback) {
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(API_URL + "movie_details.json?movie_id=" + videoId);
    requestBuilder.addHeader("Host", "xor.image.yt");
    requestBuilder.tag(MEDIA_CALL);

    return fetchDetail(requestBuilder, callback);
  }
  private void call(
      Request request,
      final Net.Parser<T> parser,
      final Net.Callback<T> callback,
      final Object tag) {
    Request.Builder builder = request.newBuilder();
    if (tag != null) builder.tag(tag);
    // 在这里添加头部文件
    LinkedHashMap<String, String> map = headers();
    if (map != null && !map.isEmpty()) {
      for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext(); ) {
        String key = iterator.next();
        builder.addHeader(key, map.get(key));
      }
      request = builder.build();
    }

    OkHttpClient client = mClient.clone();
    client.setConnectTimeout(20, TimeUnit.SECONDS);

    final Call call = client.newCall(request);
    call.enqueue(
        new Callback() {
          @Override
          public void onFailure(Request request, final IOException e) {
            final String msg = e.getMessage();
            mHandler.post(
                new Runnable() {
                  @Override
                  public void run() {
                    onError(callback, msg);
                  }
                });
          }

          @Override
          public void onResponse(final Response response) throws IOException {
            final String resp = response.body().string();
            mHandler.post(
                new Runnable() {
                  @Override
                  public void run() {
                    onNetResponse(parser, callback, resp);
                  }
                });
          }
        });
  }
  @Override
  public Call getDetail(String videoId, final Callback callback) {
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(API_URLS[CURRENT_API] + "show/" + videoId);
    requestBuilder.tag(MEDIA_CALL);

    return enqueue(
        requestBuilder.build(),
        new com.squareup.okhttp.Callback() {
          @Override
          public void onFailure(Request request, IOException e) {
            callback.onFailure(e);
          }

          @Override
          public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
              String responseStr = response.body().string();
              LinkedTreeMap<String, Object> map = mGson.fromJson(responseStr, LinkedTreeMap.class);
              EZTVReponse result = new EZTVReponse(map);
              if (map == null) {
                callback.onFailure(new NetworkErrorException("Empty response"));
              } else {
                ArrayList<Media> formattedData = result.formatDetailForPopcorn();

                if (formattedData.size() > 0) {
                  callback.onSuccess(null, formattedData, true);
                  return;
                }
                callback.onFailure(new IllegalStateException("Empty list"));
                return;
              }
            }
            callback.onFailure(new NetworkErrorException("Couldn't connect to EZTVAPI"));
          }
        });
  }
  @Override
  public Call getList(
      final ArrayList<Media> existingList, Filters filters, final Callback callback) {
    final ArrayList<Media> currentList;
    if (existingList == null) {
      currentList = new ArrayList<>();
    } else {
      currentList = (ArrayList<Media>) existingList.clone();
    }

    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("limit", "30"));

    if (filters == null) {
      filters = new Filters();
    }

    if (filters.keywords != null) {
      params.add(new NameValuePair("keywords", filters.keywords));
    }

    if (filters.genre != null) {
      params.add(new NameValuePair("genre", filters.genre));
    }

    if (filters.order == Filters.Order.ASC) {
      params.add(new NameValuePair("order", "asc"));
    } else {
      params.add(new NameValuePair("order", "desc"));
    }

    String sort = "";
    switch (filters.sort) {
      default:
      case POPULARITY:
        sort = "popularity";
        break;
      case TRENDING:
        sort = "trending";
        break;
      case YEAR:
        sort = "year";
        break;
      case DATE:
        sort = "updated";
        break;
      case RATING:
        sort = "rating";
        break;
      case ALPHABET:
        sort = "name";
        break;
    }

    params.add(new NameValuePair("sort", sort));

    String url = API_URLS[CURRENT_API] + "shows/";
    if (filters.page != null) {
      url += filters.page;
    } else {
      url += "1";
    }

    Request.Builder requestBuilder = new Request.Builder();
    String query = buildQuery(params);
    requestBuilder.url(url + "?" + query);
    requestBuilder.tag(MEDIA_CALL);

    return fetchList(currentList, requestBuilder, filters, callback);
  }
Exemplo n.º 9
0
  @Override
  public Call getList(
      final ArrayList<Media> existingList, Filters filters, final Callback callback) {
    final ArrayList<Media> currentList;
    if (existingList == null) {
      currentList = new ArrayList<>();
    } else {
      currentList = (ArrayList<Media>) existingList.clone();
    }

    ArrayList<NameValuePair> params = new ArrayList<>();
    params.add(new NameValuePair("limit", "30"));

    if (filters == null) {
      filters = new Filters();
    }

    if (filters.keywords != null) {
      params.add(new NameValuePair("query_term", filters.keywords));
    }

    if (filters.genre != null) {
      params.add(new NameValuePair("genre", filters.genre));
    }

    if (filters.order == Filters.Order.ASC) {
      params.add(new NameValuePair("order_by", "asc"));
    } else {
      params.add(new NameValuePair("order_by", "desc"));
    }

    String sort;
    switch (filters.sort) {
      default:
      case POPULARITY:
        sort = "seeds";
        break;
      case YEAR:
        sort = "year";
        break;
      case DATE:
        sort = "date_added";
        break;
      case RATING:
        sort = "rating";
        break;
      case ALPHABET:
        sort = "title";
        break;
    }

    params.add(new NameValuePair("sort_by", sort));

    if (filters.page != null) {
      params.add(new NameValuePair("page", Integer.toString(filters.page)));
    }

    Request.Builder requestBuilder = new Request.Builder();
    String query = buildQuery(params);
    requestBuilder.url(CURRENT_URL + "list_movies.json?" + query);
    requestBuilder.tag(MEDIA_CALL);

    return fetchList(currentList, requestBuilder, filters, callback);
  }