Sandeep
Sandeep

Reputation: 357

Consuming Web service from Android Application

I am a beginner on Android app development. I want to confirm if my approach is correct and as per best practices in Android world.

I have an android application that needs textual data (no graphic, video). The data comes from REST based web service as JSON string. I consume this web service using HttpGet object, parse json string using JSONArray and display data.

All this happens in a button click.

My questions are:

  1. Is there a better (or android-style) approach to do the same?
  2. What is the preferred approach to retrieve and post graphic contents to REST based web service?

Any help is most appreciated.

Thanks

Upvotes: 0

Views: 1280

Answers (2)

Ramesh Sangili
Ramesh Sangili

Reputation: 1631

Please find my inline commnets, All this happens in a button click.

My questions are:

Is there a better (or android-style) approach to do the same?

Ideal approach to trigger the Webservice calls in Async task or in a service, so your UI thread will not be blocked till HTTP fires and get the response.

What is the preferred approach to retrieve and post graphic contents to REST based web service?

The graphics content will be usually base64 when you try to retrieve it from the backend. Refer this example : http://androidtrainningcenter.blogspot.in/2012/03/how-to-convert-string-to-bitmap-and.html

for the posting the graphics to the server I'm going to assume that you know the path and filename of the image that you want to upload. Add this string to your NameValuePair using image as the key-name.

Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently 4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

You will need to add the following imports to your class.

import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

public void post(String url, List nameValuePairs) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url);

try {
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for(int index=0; index < nameValuePairs.size(); index++) {
        if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
            // If the key equals to "image", we use FileBody to transfer the data
            entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
        } else {
            // Normal string data
            entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
        }
    }

    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost, localContext);
} catch (IOException e) {
    e.printStackTrace();
}

}

Hope this helps!

Upvotes: 1

TNR
TNR

Reputation: 5869

Hey refer this link which will train you better in parsing json data as you required in Android App. Loading the graphic required some lazy loading mechanism which you can refer it from here.

Upvotes: 0

Related Questions