Android Open Source - RestIt Server Async Task






From Project

Back to project page RestIt.

License

The source code is released under:

Apache License

If you think the Android project RestIt 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 org.restit.network;
/*w  w  w. j  a va2s. co  m*/
import org.restit.model.ServerError;

import android.os.AsyncTask;

public abstract class ServerAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

  private ServerError serverError;
  private boolean connectionError;
  
  /**
   * Perform server task on background thread. This is a final method so it cannot be overridden. Implement
   * doOnServer instead. 
   */
  @Override
  protected final Result doInBackground(Params... params) {
    
    try {
      
      //execute code on server
      return doOnServer(params);
      
    } catch (ServerErrorException error) {
      
      //let the task know that an error occured
      serverError = error.getError();
      return null;
      
    } catch (NetworkNotAvailableException e)
    {
      connectionError = true;
      return null;
    }
  }
  
  /**
   * Do not implement this method unless you know what you are doing. All of your logic should
   *  go in onServerSuccess and onServerError
   */
  protected final void onPostExecute(Result result) {
    
    if(this.serverError != null)
    {
      //the server sent an error, display to user
      onServerError(this.serverError);
      
    } else if(connectionError == true)
    {
      //notify the listener that we have a connection issue
      RestIt.doTellListenerDisconnected();
    } else
    {
      RestIt.doTellListenerConnected();
      
      //everything was ok, continue with post request logic
      onServerSuccess(result);
    }
    
  };
  
  /**
   * Execute code that will call the server
   * @param params
   * @return
   */
  protected abstract Result doOnServer(Params... params) throws ServerErrorException, NetworkNotAvailableException;
  
  /**
   * Implement this method instead of onPostExecute
   * @param result
   */
  protected abstract void onServerSuccess(Result result);
  
  /**
   * Called when a proper error message is thrown from the server
   */
  protected abstract void onServerError(ServerError error);
  
  /**
   * Get the server error if one exists
   * @return
   */
  protected ServerError getServerError()
  {
    return this.serverError;
  }

}




Java Source Code List

org.restit.model.ServerError.java
org.restit.model.serialization.ServerErrorDeserializer.java
org.restit.model.serialization.ServerErrorSerializer.java
org.restit.network.AsyncCallback.java
org.restit.network.ContentType.java
org.restit.network.IRestItNetworkListener.java
org.restit.network.NetworkNotAvailableException.java
org.restit.network.NetworkUtil.java
org.restit.network.ProgressListener.java
org.restit.network.RequestMethod.java
org.restit.network.RequestOptions.java
org.restit.network.RestItClient.java
org.restit.network.RestItNetworkStatus.java
org.restit.network.RestIt.java
org.restit.network.ServerAsyncTask.java
org.restit.network.ServerErrorException.java
org.restit.network.insecure.NullHostNameVerifier.java
org.restit.network.insecure.NullX509TrustManager.java
org.restit.objectmapping.ClassRegistration.java
org.restit.objectmapping.ObjectMapping.java
org.restit.objectmapping.RestItMapper.java