REST API Client Library for Android

We are creating a location-based messaging application that uses Parse.com as a back-end (Parse.com is similar to Urban Airship / PubNub etc.), and now we want to switch to our own back-end for the best control. To do this, we built a back-end based on node.js with functionality open via the REST API

To use this API, we want to create an Android library (similar to the Parse.com Android SDK ) that abstracts all HTTP requests / Reply or the REST API calls and provides direct functions for various operations such as getUsers (), sendMessage () and etc.

Ways to implement the REST API Client on Android:

Now, given that we want to create the android library, and at the same time, REST API calls can occur when the user interacts with the application, which approach is best suited? I am open to other suggestions and recommendations.

UPDATE . First, we created our own library using IntentService + ResultReceiver, which worked great. But later, we came across Android Async Http . Use it. It's amazing!

+43
android rest asynchronous android-library intentservice
Nov 18 '12 at 20:11
source share
6 answers

The best implication I've seen based on Google IO Pro Tips 2010 is the RoboSpice library, which is based on REST and works very smartly with the Activity lifecycle so as not to leak memory.

Quick infographic library here

  • Loaders are designed for the database, not REST, they reset for reset activity means that you are losing your data.
  • Asynchronous task, just not.
  • Intent Service + The resulting receiver mainly works with RoboSpice, so if you are creating your own library, I would use this approach!
  • The service is as good as the IntentService method, but the IntentService works a little better in this case.

The Service method may be better, look at the robospice service they use the ExecutorService , which ends the Service when it finishes Requests to work, it is more Java concurrency than Android. The main thing to note is that the service starts while processing requests are completed, but they are not.

The advantage of using the ExecutorService or any type of thread pool is that you can determine how many queries you can run at once. if you have a very fast 2-4 connection, I would never have thought.

+42
Nov 18 '12 at 20:15
source share

MAYBE THIS CLASS MAY HELP: -

 /*Copyright 2014 Bhavit Singh Sengar Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/ package com.infotech.zeus.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import android.widget.Toast; public class RestClient { JSONObject data = new JSONObject(); String url; String headerName; String headerValue; public RestClient(String s){ url = s; } public void addHeader(String name, String value){ headerName = name; headerValue = value; } public void addParam(String key, String value){ try { data.put(key, value); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String executePost(){ // If you want to use post method to hit server HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader(headerName, headerValue); HttpResponse response = null; String result = null; try { StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8); httpPost.setEntity(entity); response = httpClient.execute(httpPost); HttpEntity entity1 = response.getEntity(); result = EntityUtils.toString(entity1); return result; //Toast.makeText(MainPage.this, result, Toast.LENGTH_LONG).show(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public String executeGet(){ //If you want to use get method to hit server HttpClient httpClient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); String result = null; ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { result = httpClient.execute(httpget, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } } 

A SIMPLE EXAMPLE OF USING THIS CLASS:

 RestClient client = new RestClient("http://www.example.com/demo.php"); //Write your url here client.addParam("Name", "Bhavit"); //Here I am adding key-value parameters client.addParam("Age", "23"); client.addHeader("content-type", "application/json"); // Here I am specifying that the key-value pairs are sent in the JSON format try { String response = client.executePost(); // In case your server sends any response back, it will be saved in this response string. } catch (Exception e) { e.printStackTrace(); } 
+7
Apr 09 '14 at 10:14
source share

I used Retrofit and it is a really good library that provides an easy structure for managing endpoints and analyzing data / collections / objects.

The documentation is complete enough to easily write code.

CQFD> go for it

+6
Dec 08 '14 at 21:37
source share

You can also use RESTDroid . This is very similar to RoboSpice, but easier to use (albeit less powerful).

If you are creating a Parse.com module for RESTDroid, feel free to add it to GitHub!

+4
Feb 13 '13 at 20:05
source share

If I can add one more thing, I recently started writing a good library for implementing Engines (as MKNetworkKit is used on iOS) and Commands for communicating with the REST API for Android. May be useful for those trying to use the REST API. https://github.com/m2d2/MDBaseAndroidLibraries

+1
Feb 04 '13 at 3:06
source share

You can also try Android Annotations with the rest-spring plugin. Perform these tasks automatically.

They use spring shell for android and provide a really good way to handle rest apis .

<strong> Examples:

Replace AsyncTask -> doInBackground () with @Background annotation:

 @Background protected void backgroundWork(){ // do something in background } 

Replace runOnUiThread, onPostExecute () with @UiThread

 @UiThread protected void uiWork(){ // do something on UI Thread } 

For Rest APIs

create a client for the rest:

 @Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJackson2HttpMessageConverter.class }) public interface MyRestClient { @Get("/events") EventList getEvents(); } 

use the client to relax:

 @RestClient MyRestClient myRestClient; public void showAllEvents(){ EventList list = myRestClient.getEvents(); // do something with this list } 
+1
May 6 '16 at 8:56 a.m.
source share



All Articles