コード例 #1
0
 public static void post(
     Context context, String url, StringEntity entity, AsyncHttpResponseHandler responseHandler) {
   try {
     client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
     if (false) throw new UnknownHostException();
   } catch (UnknownHostException e) {
     Log.e("sorry", "worst code ever");
   }
 }
コード例 #2
0
 public static void getCustom(
     String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
   client.get(url, params, responseHandler);
 }
コード例 #3
0
 public static void post(
     String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
   client.post(getAbsoluteUrl(url), params, responseHandler);
 }
コード例 #4
0
 public static void get(String url, AsyncHttpResponseHandler responseHandler) {
   client.get(getAbsoluteUrl(url), responseHandler);
 }
コード例 #5
0
ファイル: InstagramClient.java プロジェクト: nickai/Instagram
 public void getUserFeedSynchronously(AsyncHttpResponseHandler responseHandler) {
   String relativeUrl = "users/self/feed";
   RequestParams params = new RequestParams("access_token", client.getAccessToken().getToken());
   syncHttpClient.get(getAbsoluteUrl(relativeUrl), params, responseHandler);
 }
コード例 #6
0
    public ActionResult<Result> doInBackground() {
      String response = null;
      try {
        JsonObject jsonReq = createJSONRequest();
        Log.d(tag, "Sending JSON request to " + getUrl() + "\n" + gson.toJson(jsonReq));
        //                if("URL_REGISTER".equals(mServiceId)){
        //                    return mResult;
        //                }
        SyncHttpClient httpClient = new SyncHttpClient();
        final HttpEntity entity =
            new StringEntity(jsonReq.toString(), AsyncHttpResponseHandler.DEFAULT_CHARSET);
        httpClient.post(
            mAppContext,
            getUrl(),
            entity,
            RequestParams.APPLICATION_JSON,
            new JsonHttpResponseHandler() {
              @Override
              public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                  Header[] header = getRequestHeaders();
                  if (header != null)
                    for (int i = 0; i < header.length; i++) {
                      Log.d(tag, header[i].getName() + ": " + header[i].getValue());
                    }
                  mOriginalResponse = response;
                  Log.d(tag, "Received JSON response : " + response.toString(4));
                  super.onSuccess(statusCode, headers, response);
                  mResult = parseJSONResponse(response);
                } catch (Exception e) {
                  Log.e(tag, "Failed to process response message.", e);
                  mResult =
                      new ActionResult<>(
                          new ActionError(ErrorCode.INVALID_RESPONSE, e.getMessage()));
                }
              }

              public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                mOriginalResponse = response;
                super.onSuccess(statusCode, headers, response);
              }

              public void onSuccess(int statusCode, Header[] headers, String response) {
                mOriginalResponse = response;
                super.onSuccess(statusCode, headers, response);
              }

              public void onFailure(
                  int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                mOriginalResponse = errorResponse;
                mOriginalError = throwable;
                onFailure(
                    statusCode,
                    headers,
                    errorResponse == null ? "" : errorResponse.toString(),
                    throwable);
              }

              public void onFailure(
                  int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                mOriginalResponse = errorResponse;
                mOriginalError = throwable;
                onFailure(
                    statusCode,
                    headers,
                    errorResponse == null ? "" : errorResponse.toString(),
                    throwable);
              }

              @Override
              public void onFailure(
                  int statusCode, Header[] headers, String responseString, Throwable throwable) {
                mOriginalResponse = responseString;
                mOriginalError = throwable;
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e(
                    tag,
                    "Received Error response : StatusCode: "
                        + statusCode
                        + ", Received response : "
                        + responseString,
                    throwable);
                try {
                  if (statusCode >= HttpStatus.SC_BAD_REQUEST
                      && statusCode < HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    mResult =
                        new ActionResult<>(
                            new ActionError(ErrorCode.INVALID_REQUEST, responseString));
                  } else if (statusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                    mResult =
                        new ActionResult<>(new ActionError(ErrorCode.SERVER_ERROR, responseString));
                  } else {
                    mResult =
                        new ActionResult<>(
                            new ActionError(
                                ErrorCode.NETWORK_ERROR,
                                mAppContext.getString(R.string.network_error)));
                  }
                } catch (Exception e) {
                  Log.e(tag, "Failed to process response message.", e);
                  mResult =
                      new ActionResult<>(
                          new ActionError(
                              ErrorCode.INVALID_RESPONSE,
                              mAppContext.getString(R.string.unknown_error)));
                }
              }

              protected Object parseResponse(byte[] responseBody) throws JSONException {
                if (null == responseBody) return null;
                Object result = null;
                // trim the string to prevent start with blank, and test if the string is valid
                // JSON, because the parser don't do this :(. If JSON is not valid this will return
                // null
                String jsonString = getResponseString(responseBody, getCharset());
                if (jsonString != null) {
                  Log.d(tag, "Original jsonString: " + jsonString);
                  jsonString = jsonString.trim();
                  while (jsonString.length() > 1 && !jsonString.startsWith("{")) {
                    jsonString = jsonString.substring(1);
                  }
                  if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
                    result = new JSONTokener(jsonString).nextValue();
                    mGsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
                  }
                }
                if (result == null) {
                  result = jsonString;
                }
                return result;
              }
            });
      } catch (Exception e) {
        Log.e(tag, "Failed to process action : " + mServiceId + "\n" + response, e);
        mOriginalError = e;
        mResult =
            new ActionResult<Result>(
                new ActionError(
                    ErrorCode.NETWORK_ERROR, mAppContext.getString(R.string.network_error)));
      }
      if (mBackgroundCallBack != null) {
        if (mResult.hasError()) {
          mBackgroundCallBack.onFailure(mResult.getError());
        } else {
          mBackgroundCallBack.onSuccess(mResult.getObject());
        }
      }
      return mResult;
    }
コード例 #7
0
ファイル: HttpEngine.java プロジェクト: thekey123/andthink
 /** 使用原生同步请求的方式访问服务器 */
 public void SynPost(AsyncHttpResponseHandler handler) {
   syncHttpClient.post(url, params, handler);
 }