Ejemplo n.º 1
0
  public static GetJsonResponse makeRequest(String resource) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) ApiUtils.endpoint(resource).openConnection();

    try {
      connection.setRequestProperty("Accept", Utils.MIME_JSON);

      InputStream in = new BufferedInputStream(connection.getInputStream());
      String responseBody = Utils.inputStreamToString(in);
      in.close();

      return new GetJsonResponse(connection.getResponseCode(), responseBody);
    } finally {
      connection.disconnect();
    }
  }
  public static PostJsonResponse makeRequest(String resource, String contents) throws IOException {
    contents = (contents == null) ? "" : contents;

    HttpURLConnection connection = (HttpURLConnection) ApiUtils.endpoint(resource).openConnection();

    try {
      connection.setDoOutput(true); // sets this to make a POST request
      connection.setRequestProperty("Accept", Utils.MIME_JSON);
      connection.setRequestProperty("Content-Type", Utils.MIME_FORM);
      connection.setRequestProperty("Content-Length", String.valueOf(contents.length()));

      OutputStream out = new BufferedOutputStream(connection.getOutputStream());
      out.write(contents.getBytes());
      out.close();

      InputStream in = new BufferedInputStream(connection.getInputStream());
      String responseBody = Utils.inputStreamToString(in);
      in.close();

      return new PostJsonResponse(connection.getResponseCode(), responseBody);
    } finally {
      connection.disconnect();
    }
  }
Ejemplo n.º 3
0
 public static GetJsonResponse makeRequest(String resource, Params urlParams) throws IOException {
   return makeRequest(ApiUtils.concatQuery(resource, urlParams));
 }