@Override
 public void run() {
   try {
     HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
     this.setupSecurity(request);
     request.acceptCharset(CHARSET);
     request.headers(this.getHeaders());
     JSONObject headers = this.parseHeaders(request.headers());
     int code = request.code();
     String body = request.body(CHARSET);
     JSONObject response = new JSONObject();
     response.put("status", code);
     response.put("headers", headers);
     if (code >= 200 && code < 300) {
       response.put("data", body);
       this.getCallbackContext().success(response);
     } else {
       response.put("error", body);
       this.getCallbackContext().error(response);
     }
   } catch (JSONException e) {
     this.respondWithError("There was an error generating the response");
   } catch (HttpRequestException e) {
     if (e.getCause() instanceof UnknownHostException) {
       this.respondWithError(0, "The host could not be resolved");
     } else if (e.getCause() instanceof SSLHandshakeException) {
       this.respondWithError("SSL handshake failed");
     } else {
       this.respondWithError("There was an error with the request");
     }
   }
 }
Esempio n. 2
0
 /** @see org.sonar.process.test.HttpProcess */
 boolean isReady() {
   try {
     HttpRequest httpRequest =
         HttpRequest.get("http://localhost:" + httpPort + "/" + "ping")
             .readTimeout(2000)
             .connectTimeout(2000);
     return httpRequest.ok() && httpRequest.body().equals("ping");
   } catch (HttpRequest.HttpRequestException e) {
     return false;
   }
 }
 /**
  * Get all bootstrap Checkins that exists on Parse.com
  *
  * @return non-null but possibly empty list of bootstrap
  * @throws IOException
  */
 public List<CheckIn> getCheckIns() throws IOException {
   try {
     final HttpRequest request = execute(HttpRequest.get(URL_CHECKINS));
     final CheckInWrapper response = fromJson(request, CheckInWrapper.class);
     if (response != null && response.results != null) {
       return response.results;
     }
     return Collections.emptyList();
   } catch (final HttpRequestException e) {
     throw e.getCause();
   }
 }
 /**
  * Get all bootstrap Users that exist on Parse.com
  *
  * @return non-null but possibly empty list of bootstrap
  * @throws IOException
  */
 public List<User> getUsers() throws IOException {
   try {
     final HttpRequest request = execute(HttpRequest.get(URL_USERS));
     final UsersWrapper response = fromJson(request, UsersWrapper.class);
     if (response != null && response.results != null) {
       return response.results;
     }
     return Collections.emptyList();
   } catch (final HttpRequestException e) {
     throw e.getCause();
   }
 }
 private HttpRequest createRequest(String source) {
   HttpRequest request = HttpRequest.get(source);
   // Add credentials if a secure connection to github.com
   HttpURLConnection connection = request.getConnection();
   if (connection instanceof HttpsURLConnection
       && HOST_DEFAULT.equals(connection.getURL().getHost())) {
     Account account = AccountUtils.getAccount(context);
     if (account != null) {
       String password = AccountManager.get(context).getPassword(account);
       if (!TextUtils.isEmpty(password)) request.basic(account.name, password);
     }
   }
   return request;
 }
    public static String getJSONFromUrl(Activity activity, String url) throws CustomException {
      String json;
      // defaultHttpClient
      try {
        json = HttpRequest.get(url).body();

        if (json != null && json.contains("listeSerie")) {
          FileUtils.saveContent(activity, json);
        }
      } catch (HttpRequest.HttpRequestException e) {
      }
      json = FileUtils.loadFile(activity);
      // return JSON String
      return json;
    }
  /**
   * Makes HTTP get request and parses the responses JSON into HashMaps.
   *
   * @throws IOException
   */
  private void getData() throws IOException {
    String json = HttpRequest.get(URL).body();
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createJsonParser(json);

    Map<String, Object> region = null;

    parser.nextToken();
    while (parser.nextToken() == JsonToken.START_OBJECT) {
      region = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {});
      if (region.containsKey(CATEGORY_FIELD)) {
        if (REGION_CATEGORY.equals(region.get(CATEGORY_FIELD))) {
          regionsByCode.put((String) region.get(CODE_FIELD), (Integer) region.get(ID_FIELD));
          regionsById.put((Integer) region.get(ID_FIELD), (String) region.get(CODE_FIELD));
        }
      }
    }
  }
Esempio n. 8
0
 @Test
 public void getHelpIsWorking() throws Exception {
   HttpRequest helpRequest = HttpRequest.get(CONTAINER_URL + PATH + "help");
   assertThat(helpRequest.body()).isEqualTo("This is working");
 }
 public static String get(String url) {
   String response = HttpRequest.get(url).body();
   return response;
 }