56

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.

Drunken Daddy
  • 7,326
  • 14
  • 70
  • 104

1 Answers1

79
  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.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
Yogendra
  • 4,817
  • 1
  • 28
  • 21
  • Thank you. strikeon link seems easy, I'm going to try that – Drunken Daddy Mar 30 '15 at 08:00
  • 1
    this tutorial uses http get() method. what if I want to use post()? – Drunken Daddy Mar 30 '15 at 10:14
  • http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/ – Yogendra Mar 30 '15 at 10:18
  • 3
    and also check my ans. i have update it – Yogendra Mar 30 '15 at 10:25
  • I'm sorry, but it is too difficult for me to understand. – Drunken Daddy Mar 30 '15 at 10:36
  • 1. Copy a jar file from http://loopj.com/android-async-http/ and place in lib folder. 2.Place class HttpUtils in your source folder. 3. Call method where you want to call api. – Yogendra Mar 30 '15 at 10:40
  • 11
    I would have suggested [retrofit](http://square.github.io/retrofit/). I can't think of an easier way for consuming a REST api on Android. Anyway, I'm leaving this here for future users that get here. – LeoFarage May 15 '16 at 20:42
  • What's AppConstant.URL_FEED? It wasn't found here. Using the latest Android Studio, minSdkVersion 16 and targetSdkVersion 24. – Rasshu Jun 28 '16 at 18:41
  • AppConstant.URL_FEED this is relative url, this is using in my app by constant string. u can use by constant file or direct give API – Yogendra Jul 03 '16 at 07:53
  • Hi Yogendra, is it safe to called multiple requests using the same `AsyncHttpClient client` even though it hasn't return anything on previous request? Or I should create multiple `AsyncHttpClient`? Is it a big overhead to create multiple `AsyncHttpClient`? – Sam Feb 08 '19 at 03:02
  • you can call multiple request because all in bg thread – Yogendra Feb 08 '19 at 03:20