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.
Asked
Active
Viewed 1.8e+01k times
56
-
Search for `android http library` in most cases they have nice examples – Sir l33tname Mar 30 '15 at 06:23
-
http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/ – Dinesh Kannan Mar 30 '15 at 06:24
1 Answers
79
- If you want to integrate Retrofit (all steps defined here):
Goto my blog : retrofit with kotlin
- Please use android-async-http library.
the link below explains everything step by step.
http://loopj.com/android-async-http/
Here are sample apps:
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.
-
-
1this 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
-
-
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
-
11I 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
-