Exemplo n.º 1
0
  private JSONObject makeBitfluRequest(Log log, String addToUrl) throws DaemonException {

    try {

      // Initialise the HTTP client
      if (httpclient == null) {
        initialise();
      }

      // TLog.d(LOG_NAME, "Request to: "+ buildWebUIUrl() + addToUrl);

      // Make request
      HttpGet httpget = new HttpGet(buildWebUIUrl() + addToUrl);
      HttpResponse response = httpclient.execute(httpget);

      // Read JSON response
      InputStream instream = response.getEntity().getContent();
      String result = HttpHelper.convertStreamToString(instream);
      int httpstatus = response.getStatusLine().getStatusCode();

      if (httpstatus != 200) {
        throw new DaemonException(
            ExceptionType.UnexpectedResponse,
            "Invalid reply from server, http status code: " + httpstatus);
      }

      if (result.equals("")) { // Empty responses are ok: add fake json content
        result = "empty_response";
      }

      JSONObject json = new JSONObject("{ \"" + JSON_ROOT + "\" : " + result + "}");

      instream.close();
      return json;

    } catch (DaemonException e) {
      throw e;
    } catch (JSONException e) {
      log.d(LOG_NAME, "Error: " + e.toString());
      throw new DaemonException(ExceptionType.ParsingFailed, e.toString());
    } catch (Exception e) {
      log.d(LOG_NAME, "Error: " + e.toString());
      throw new DaemonException(ExceptionType.ConnectionError, e.toString());
    }
  }
Exemplo n.º 2
0
  /**
   * Instantiates an HTTP client with proper credentials that can be used for all HTTP requests.
   *
   * @throws DaemonException On conflicting or missing settings
   */
  private void initialise() throws DaemonException {

    httpclient = HttpHelper.createStandardHttpClient(settings, true);
  }