Back to project page RestLib.
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.
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); } }