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.
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.
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.
Goto my blog : retrofit with kotlin
the link below explains everything step by step.
http://loopj.com/android-async-http/
Here are sample apps:
http://www.techrepublic.com/blog/software-engineer/calling-restful-services-from-your-android-app/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With