@Override
    public void handleMessage(final Message msg) {
      final ApplicationService appService = ApiMethodsImpl.this.appService;
      if (appService == null) {
        return;
      } // we are destroyed

      if (msg.what == MSG_FINISH) {
        workingFlag.set(false);
        appService.checkForStop();
        return;
      }
      workingFlag.set(true);

      final Application app = appService.getApp();
      final RequestMethodHelper h = app.getRequestMethodHelper();

      final RequestDescription description = (RequestDescription) msg.obj;

      final ParserContext pContext = h.createParserContext(description);
      pContext.setSystemContext(appService);
      final int opCode = description.getOperationCode();
      if (DEBUG) {
        Log.d(TAG, "Current context: " + pContext + ", op " + opCode);
      }
      final int token = description.getToken();

      before(description, pContext, appService);

      pending.set(NULL_OPERATION_DATA);
      pending.set(opCode, token);
      try {
        // execute request method
        final RequestMethod rm = h.createRequestMethod(description);
        rm.setup(app);
        rm.start(appService, description, pContext);
        rm.stop(app);

        // process results
        final ResponseData response = pContext.processResults();

        // report results
        if (pContext.isSuccessful()) {
          reportApiSuccess(token, opCode, response);
        } else {
          Log.e(TAG, "Server error: " + response.getErrorCode() + ", " + response.getMessage());
          reportError(token, opCode, response);
        }

      } catch (final RequestMethodException e) {
        Log.e(TAG, "Request method error", e);
        pContext.defineResponse(e);
        reportError(token, opCode, pContext.processResults());
      } finally {
        after(description, pContext, appService);
        dumpLastOperation(lastOperation);
        pending.set(NULL_OPERATION_DATA);
        pContext.destroy();
      }
    }
Пример #2
0
  @Override
  public void sendRequest(
      final Context context,
      final URLConnection connection,
      final RequestDescription requestDescription)
      throws IOException {
    final LinkedList<ParameterValue> parameters = new LinkedList<ParameterValue>();
    for (final Parameter p : requestDescription.getSimpleParameters().getChildren()) {
      if (p instanceof ParameterValue) {
        parameters.add((ParameterValue) p);
      }
    }
    final String encoding = requestDescription.getEncoding().name();
    final byte[] content = URLEncodedUtils.format(parameters, encoding).getBytes(encoding);

    final OutputStream stream = connection.getOutputStream();
    stream.write(content);
    stream.flush();

    if (DEBUG) {
      Log.d(TAG, "(" + requestDescription.getId() + ")" + ": " + parameters.toString());
    }
  }