Consuming Rest API in Android
Consuming Rest API in Android
1:Introduction
A REST API means application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.
2:Sync ASync
3:Installation
//in your project's app level build.gradle
dependencies {
compile 'com.android.volley:volley:x.y.z'
}
4:MANIFESTS/ANDROIDMANIFEST.XML
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
5:Basic classes of Volley
RequestQueue
JsonArrayRequest
JsonObjectRequest
5.1 -> To use volley first you need to create object of RequestQueue
RequestQueue vmQueue = Volley.newRequestQueue(getApplicationContext());
5.2 -> make a Request using either JsonArrayRequest or JsonObjectRequest
JsonArrayRequest mJsonRequest = new JsonArrayRequest(url,
new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// here you can parse response and use accordingly
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// here you will receive errors and show proper message according to error type
}
});
5.3 -> put the request in queue. i.e.
vmQueue.add(mJsonRequest);
Instead of doing the above steps, you can create a singleton class VolleyRequestQuery
6:Create VolleyRequestQuery class
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class VolleyRequestQuery {
private static VolleyRequestQuery mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private VolleyRequestQuery(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized VolleyRequestQuery getInstance(Context context) {
// If instance is not available, create it. If available, reuse and return the object.
if (mInstance == null) {
mInstance = new VolleyRequestQuery(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key. It should not be activity context,
// or else RequestQueue won’t last for the lifetime of your app
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public void addToRequestQueue(Request req) {
getRequestQueue().add(req);
}
}
You can now use the VolleyRequestQuery in your MainActivity like this
VolleyRequestQuery.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);.
Or you can create a queue in this way:
RequestQueue queue = VolleyRequestQuery.getInstance(this.getApplicationContext()).getRequestQueue();.
7:For example If you want to retrive posts through web api then
String URL = "https://jsonplaceholder.typicode.com/posts";
public void getPost(int id) {
JsonObjectRequest postsList = new JsonObjectRequest(Request.Method.GET,
URL + "/" + id,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//your code goes here
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//error code goes here
}
});
}
8:Posting to the server
final HashMap<String, String> postParams = new HashMap<String, String>();
postParams.put("username", username);
postParams.put("password", password);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
"YOUR URL WITH JSON DATA",
jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
if (response.getString("status").equals("fail")) {
} else if (response.getString("status").equals("success")) {
} catch (JSONException e) {
Log.e("TAG", e.toString())
}
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
//pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
VolleyRequestQuery.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);
}
9: Putting it all together