Beispiel #1
0
  private void log(String message, Throwable e) {
    if (e != null) {
      Log.e(TAG, message, e);

    } else {
      Log.i(TAG, message);
    }
  }
Beispiel #2
0
  private void doGetAdList(final AsyncTaskPayload payload) {
    AdverRequest request = (AdverRequest) payload.getData()[0];

    String jsonStr = JSONHelper.objToJson(request);

    HttpEntity entity = new ByteArrayEntity(jsonStr.getBytes());
    final String url = NetworkHelper.executeUrl(ServiceConstants.SERVICE_AD);
    Log.i(TAG + " url: =====>", url);
    Log.i(TAG + " jsonStr: =====>", jsonStr);
    NetworkHelper.asyncHttpClient.post(
        mContext,
        url,
        entity,
        "application/json; charset=utf-8",
        new AsyncHttpResponseHandler() {
          @Override
          public void onSuccess(String content) {
            super.onSuccess(content);
            Log.i(TAG + " Response: =====>", content);
            BaseResponse response = null;
            response = NetworkHelper.doObject(url, content, AdverResponse.class);
            if (checkErrorResponse(payload, response, false)) {
              return;
            }
            if (response instanceof AdverResponse) {
              mAdList = ((AdverResponse) response).getData();
            }
            dismisDialog();
            mListener.onSuccess(payload.getTaskType());
          }

          @Override
          protected void handleFailureMessage(Throwable e, String responseBody) {
            err = responseBody;
            super.handleFailureMessage(e, responseBody);
          }

          @Override
          public void onFailure(Throwable error) {
            super.onFailure(error);
            if (StringUtils.isEmpty(err)) {
              onfailure(payload);
            } else {
              onfailures(payload, err);
            }
          }
        });
  }
Beispiel #3
0
  private synchronized void start() {
    log("Starting service...");

    // Do nothing, if the service is already running.
    if (mStarted == true) {
      Log.w(TAG, "Attempt to start connection that is already active");
      return;
    }

    // Establish an MQTT connection
    connect();

    // Register a connectivity listener
    registerReceiver(
        mConnectivityChanged, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
  }
Beispiel #4
0
  private synchronized void stop() {
    // Do nothing, if the service is not running.
    if (mStarted == false) {
      Log.w(TAG, "Attempt to stop connection not active.");
      return;
    }

    // Save stopped state in the preferences
    setStarted(false);

    // Remove the connectivity receiver
    unregisterReceiver(mConnectivityChanged);
    // Any existing reconnect timers should be removed, since we explicitly
    // stopping the service.
    cancelReconnect();

    // Destroy the MQTT connection if there is one
    if (mConnection != null) {
      mConnection.disconnect();
      mConnection = null;
    }
  }