Пример #1
0
  public void build(String url, UploadCallback callback) {
    try {
      for (Map.Entry<String, Object> entry : params.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof String)
          builder.addPart(name, new StringBody((String) value, Consts.UTF_8));
        else if (value instanceof File)
          builder.addPart(
              name,
              new FileBody((File) value, ContentType.create("image/*"), ((File) value).getName()));
      }

      HttpPost post = new HttpPost(url);
      builder.setCharset(Consts.UTF_8);
      post.setEntity(builder.build());

      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(post);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String responseBody = EntityUtils.toString(response.getEntity());
        if (callback != null) callback.onSuccess(JSON.parseObject(responseBody));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  public static String apiRequest(Request request, String... paramValues) {
    if (httpClient == null) {
      httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    }
    RequestBuilder reqBuilder;
    HttpUriRequest get, post;
    switch (request.type) {
      case GET:
        reqBuilder = RequestBuilder.get("http://naturesnap.net/" + request.getEndpoint());
        for (int i = 0; i < request.getParams().length; i++) {
          reqBuilder.addParameter(request.getParams()[i], paramValues[i]);
        }
        get = reqBuilder.build();
        try {
          CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(get);
          HttpEntity entity = response.getEntity();
          String content = IOUtils.toString(entity.getContent(), "UTF-8");
          response.close();
          return content;
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
      case POST:
        reqBuilder = RequestBuilder.post().setUri("http://naturesnap.net/" + request.getEndpoint());
        for (int i = 0; i < request.getParams().length; i++) {
          reqBuilder.addParameter(request.getParams()[i], paramValues[i]);
        }
        post = reqBuilder.build();
        try {
          CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(post);
          HttpEntity entity = response.getEntity();
          String content = IOUtils.toString(entity.getContent(), "UTF-8");
          response.close();
          return content;
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
      case FILE:
        HttpPost postFile = new HttpPost("http://naturesnap.net/" + request.getEndpoint());
        File file = new File(request.getParams()[0]);
        FileBody fileBody = new FileBody(file);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("filename", fileBody);
        builder.addTextBody("uploadForm", "Upload");
        builder.addTextBody("latitude", request.getParams()[1]);
        builder.addTextBody("longitude", request.getParams()[2]);
        builder.addTextBody("groupName", "");
        builder.addTextBody("description", "");
        HttpEntity entity = builder.build();
        postFile.setEntity(entity);
        try {
          HttpResponse response = httpClient.execute(postFile);
          return IOUtils.toString(response.getEntity().getContent(), "UTF-8");
        } catch (Exception e) {

        }
        break;
      default:
        // Error
        break;
    }

    return "";
  }