Android Open Source - android-rest-client Custom Handlers Request






From Project

Back to project page android-rest-client.

License

The source code is released under:

Apache License

If you think the Android project android-rest-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dg.examples.restclientdemo.communication.requests;
//  www.j a  v  a 2  s . c o  m
import android.os.Handler;
import android.os.Looper;

import com.araneaapps.android.libs.asyncrunners.enums.DownloadPriority;
import com.araneaapps.android.libs.asyncrunners.models.RequestOptions;
import com.dg.libs.rest.callbacks.HttpCallback;
import com.dg.libs.rest.client.RequestMethod;
import com.dg.libs.rest.domain.ResponseStatus;
import com.dg.libs.rest.handlers.UIThreadResponseHandler;
import com.dg.libs.rest.requests.RestClientRequest;

public class CustomHandlersRequest extends RestClientRequest<Void> {

  private final CustomHandlersRequest.CustomUIHandler handler;
  private VoidHttpCallback callback;

  public CustomHandlersRequest(VoidHttpCallback callback) {
    super();
    this.callback = callback;
    setRequestMethod(RequestMethod.GET);
    setUrl("http://some-dummy-url.com");

    handler = new CustomUIHandler(callback);
    setResponseHandler(handler);
    // You can add custom request options for specific request. there is a queue running the requests so new requests coming in
    // will be sorted and executed according to priority if needed.
    // ex. you queue 50 downloads but you want the app to still run API get requests without waiting for everything else to finish first.
    RequestOptions requestOptions = new RequestOptions.RequestOptionsBuilder()
        .setPriority(DownloadPriority.HIGH)
        .setRunInSingleThread(true).build();
    setRequestOptions(requestOptions);
  }

  @Override
  protected boolean handleResponseStatus(final ResponseStatus status) {
    // This method will also run in the background thread. Returning true means you have handled the request and this will be
    // the last piece of code which will be executed.
    // False means the execution will continue with the parser and then success if the request is parsed successfully.
    if (status.getStatusCode() == 204) {

      callback.onCustomResult(status);// careful this runs in Background thread.

      // Or to run in UI thread:
      Handler handler = new Handler(Looper.getMainLooper());
      handler.post(new Runnable() {
        @Override
        public void run() {
          callback.onCustomResult(status); // This will run in UI thread.
        }
      });

      // Alternatively:
      CustomHandlersRequest.this.handler.handleCustom(status);
      // will also run in UI thread but needs extra logic to implement as opposed to the code above. Cleaner but a bit more complex.

      return true;
    } else {
      return super.handleResponseStatus(status);
    }
  }

  private class CustomUIHandler extends UIThreadResponseHandler<Void> {

    private VoidHttpCallback callback;

    public CustomUIHandler(VoidHttpCallback callback) {
      super(callback);
      this.callback = callback;
    }

    public void handleCustom(final ResponseStatus status) {
      handler.post(new Runnable() {
        @Override
        public void run() {
          callback.onCustomResult(status); // This will run in UI thread.
        }
      });
    }
  }

  @Override
  protected void doAfterSuccessfulRequestInBackgroundThread(Void data) {
    super.doAfterSuccessfulRequestInBackgroundThread(data);
  }

  @Override
  protected void doBeforeRunRequestInBackgroundThread() {
    // this will run before the request runs. here you can query databases, add parameters or headers in the request background thread.
    // So there is no need to do async tasks or similar stuff to fetch parameters before executing a request if needed.
    super.doBeforeRunRequestInBackgroundThread();
  }

  private static class VoidHttpCallback implements HttpCallback<Void> {

    @Override
    public void onSuccess(Void responseData, ResponseStatus responseStatus) {

    }

    @Override
    public void onHttpError(ResponseStatus responseCode) {

    }

    public void onCustomResult(ResponseStatus status) {
    }
  }
}




Java Source Code List

com.dg.examples.restclientdemo.MainActivity.java
com.dg.examples.restclientdemo.app.App.java
com.dg.examples.restclientdemo.communication.RestConstants.java
com.dg.examples.restclientdemo.communication.parsers.BlogsGoogleParser.java
com.dg.examples.restclientdemo.communication.requests.BlogsGoogleRequest.java
com.dg.examples.restclientdemo.communication.requests.CustomHandlersRequest.java
com.dg.examples.restclientdemo.communication.requests.PatchRequest.java
com.dg.examples.restclientdemo.domain.EntriesModel.java
com.dg.examples.restclientdemo.domain.ResponseDataModel.java
com.dg.examples.restclientdemo.domain.ResponseModel.java
com.dg.libs.rest.HttpRequest.java
com.dg.libs.rest.RestClientConfiguration.java
com.dg.libs.rest.authentication.AuthenticationProvider.java
com.dg.libs.rest.authentication.TokenAuthenticationProvider.java
com.dg.libs.rest.callbacks.HttpCallback.java
com.dg.libs.rest.client.RequestMethod.java
com.dg.libs.rest.domain.ResponseStatus.java
com.dg.libs.rest.entities.CountingInputStreamEntity.java
com.dg.libs.rest.entities.UnicodeBOMInputStream.java
com.dg.libs.rest.exceptions.HttpException.java
com.dg.libs.rest.exceptions.ParseException.java
com.dg.libs.rest.handlers.BackgroundThreadResponseHandler.java
com.dg.libs.rest.handlers.DefaultResponseStatusHandler.java
com.dg.libs.rest.handlers.ResponseHandler.java
com.dg.libs.rest.handlers.ResponseStatusHandler.java
com.dg.libs.rest.handlers.UIThreadResponseHandler.java
com.dg.libs.rest.parsers.BaseJacksonMapperResponseParser.java
com.dg.libs.rest.parsers.FileHttpResponseParser.java
com.dg.libs.rest.parsers.HttpResponseParser.java
com.dg.libs.rest.parsers.NoResponseParser.java
com.dg.libs.rest.parsers.StringHttpResponseParser.java
com.dg.libs.rest.requests.RequestBodyUtils.java
com.dg.libs.rest.requests.RestClientRequest.java