Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call REST API from an android app? [closed]

Tags:

rest

android

I'm new to android and new to programming as well. How do I call a REST api (GET/POST request) from an android app. Please suggest me a good tutorial, or give me an idea to start with.

like image 478
Drunken Daddy Avatar asked Mar 30 '15 06:03

Drunken Daddy


People also ask

Can you use REST API for mobile app?

Overall, RESTful APIs enhance web apps and mobile apps that are distributed over the Internet. The mobile apps become more scalable and it's easier to modify it as well. Ultimately, your app will become more reliable, portable, visible, and simplified with a RESTful API.

Can a browser call a REST API?

The REST API uses several HTTP methods to perform various actions on REST resources. Any REST API call that uses the HTTP GET method can be submitted using a Web browser such as Microsoft Internet Explorer or Mozilla Firefox.


1 Answers

  1. If you want to integrate Retrofit (all steps defined here):

Goto my blog : retrofit with kotlin

  1. Please use android-async-http library.

the link below explains everything step by step.

http://loopj.com/android-async-http/

Here are sample apps:

  1. http://www.techrepublic.com/blog/software-engineer/calling-restful-services-from-your-android-app/

  2. http://blog.strikeiron.com/bid/73189/Integrate-a-REST-API-into-Android-Application-in-less-than-15-minutes

Create a class :

public class HttpUtils {   private static final String BASE_URL = "http://api.twitter.com/1/";     private static AsyncHttpClient client = new AsyncHttpClient();    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {       client.get(getAbsoluteUrl(url), params, responseHandler);   }    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {       client.post(getAbsoluteUrl(url), params, responseHandler);   }          public static void getByUrl(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {       client.get(url, params, responseHandler);   }    public static void postByUrl(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {       client.post(url, params, responseHandler);   }    private static String getAbsoluteUrl(String relativeUrl) {       return BASE_URL + relativeUrl;   } } 

Call Method :

    RequestParams rp = new RequestParams();     rp.add("username", "aaa"); rp.add("password", "aaa@123");                          HttpUtils.post(AppConstant.URL_FEED, rp, new JsonHttpResponseHandler() {         @Override         public void onSuccess(int statusCode, Header[] headers, JSONObject response) {             // If the response is JSONObject instead of expected JSONArray             Log.d("asd", "---------------- this is response : " + response);             try {                 JSONObject serverResp = new JSONObject(response.toString());                                                             } catch (JSONException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }                            }                      @Override         public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {             // Pull out the first event on the public timeline                              }     }); 

Please grant internet permission in your manifest file.

 <uses-permission android:name="android.permission.INTERNET" /> 

you can add compile 'com.loopj.android:android-async-http:1.4.9' for Header[] and compile 'org.json:json:20160212' for JSONObject in build.gradle file if required.

like image 142
Yogendra Avatar answered Sep 18 '22 14:09

Yogendra