Android Open Source - RestLib Rest Service






From Project

Back to project page RestLib.

License

The source code is released under:

MIT License

If you think the Android project RestLib 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 github.crazymumu.restlib;
/*from   ww  w  .  ja va  2 s . c o m*/
import github.crazymumu.restlib.core.HttpsClient;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;

/**
 * RestService handles asynchronous RESTful web API requests on demand. Clients
 * implement RestResult interface, setup RestResultReceiver, and send requests
 * through request(Context, RestRequest, RestResultReceiver) calls; the service
 * is started as needed, handles each Intent in turn using a worker thread, and
 * stops itself when it runs out of work.
 * 
 * @author Crazy MuMu
 *
 */
public class RestService extends IntentService {
  public final static String REQUEST = "request";
  public final static String RECEIVER = "receiver";
  public final static String RESULT = "result";
  
  public static void request(Context context, RestRequest request, RestResultReceiver receiver) {
    Intent intent = new Intent(context, RestService.class);
    intent.putExtra(REQUEST, request);
    intent.putExtra(RECEIVER, receiver);
    context.startService(intent);
  }
  
  public RestService() {
    super("RestService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    RestRequest request = intent.getParcelableExtra(REQUEST);
    HttpsClient client = new HttpsClient();
    RestResponse response = client.execute(request);
    ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
    Bundle result = new Bundle();
    result.putParcelable(RESULT, response);
    receiver.send(0, result);
  }
}




Java Source Code List

github.crazymumu.restlib.RestRequest.java
github.crazymumu.restlib.RestResponse.java
github.crazymumu.restlib.RestResultReceiver.java
github.crazymumu.restlib.RestResult.java
github.crazymumu.restlib.RestService.java
github.crazymumu.restlib.core.CustomSSLSocketFactory.java
github.crazymumu.restlib.core.HttpsClient.java
github.crazymumu.restlib.core.SSLContextAlgorithm.java
github.crazymumu.restlib.http.RequestMethod.java
github.crazymumu.restlib.http.StatusCode.java
github.crazymumu.restlib.http.UserAgent.java
github.crazymumu.restlib.sample.MainActivity.java