user3240944
user3240944

Reputation: 331

what kinds of REST services work with Android

I need to make a decision on what kind of REST service needs to be created for a web service which must receive a URL with patient data and return back insurance information which he can use. This service must work with Android.

I understand from some previous posts, that JAXB is not a good idea. SOAP is really not good for android..

Therefore, my question is - what kind of Web Service is appropriate for Android ?

Upvotes: 0

Views: 68

Answers (2)

Quentin Klein
Quentin Klein

Reputation: 1393

If I understand, you need a Webservice to communicate with your app and it'll be like:

Your app calls your WS url (maybe with some POST datas) and your WS responds to your app.

Then, you can make a REST Webservice that communicates in JSON? Android can parse JSON with some very good libraries like Google GSON for example.

For the WS technology you can use whatever you want.

The things will work like, you ask for an object, lets say it's a list of people with an age and a name. The API is at http://www.yourapi.com/, so as you're asking for people, it's http://www.yourapi.com/people

When you call the API, the response is like:

[{
    "age": 18,
    "name": "toto"
},
{
    "age": 21,
    "name": "foo"
}]

So, from a client side (your Android project), you have a Person class: Person.java

public class Person {
    private int age;
    private String name;

    public Person(){
        this.age = 0;
        this.name = "";
    }

    public int getAge(){
         return age;
    }

    public void setAge(int age){
         this.age = age;
    }

    public String getName({
         return name;
    }

    public void setName(String name){
         this.name = name;
    }
}

Which is your model, and an AsyncTask to download it:

public abstract class PeopleDownloader extends AsyncTask<Integer, Integer, ArrayList<Person>> {

    private static final String URL = "http://www.yourapi.com/people";

    @Override
    protected ArrayList<Person> doInBackground(Void... voids) {
        HttpClient httpclient = new DefaultHttpClient();
        // Prepare a request object
        HttpGet httpget = new HttpGet(URL);
        // Execute the request
        HttpResponse response;
        try {
            Log.d(TAG, "Trying to reach " + httpget.getURI());
            response = httpclient.execute(httpget);
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String finalResposne = "";
            String line = "";
            while ((line = rd.readLine()) != null) {
                finalResposne += line;
            }
            Log.d(TAG, finalResposne);
            return new Gson().fromJson(finalResposne, new TypeToken<List<Person>>() {
            }.getType());
        } catch (Exception e) {
            Log.e(TAG, "Error in doInBackground " + e.getMessage());
        }
        return null;
    }
}

And it's done.

Upvotes: 1

Carsten
Carsten

Reputation: 4334

JSON works well using the Jackson library. GSON may be an option but I haven't tried it myself.

SOAP is pretty much out of the question as you mention yourself, but XML binding over REST can be done reasonably well using the SimpleXML framework instead of JAXB. For complex object architectures this can be quite slow however. If you have the choice I'd say go for JSON.

Have a look at http://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats for additional options.

Upvotes: 1

Related Questions