Volley
Get Start
import library
Open build.gradle and Add volley Support by Adding compile 'com.android.volley:volley:1.+'
Adding Permission
In AndroidManifests.xml add INTERNET Permission
<uses-permission android:name="android.permission.INTERNET"/>
Usage
Init RequestQueue RequestQueue mQueue = Volley.newRequestQueue(this);
Get
// String Request
StringRequest getRequest = new StringRequest("https://jsonplaceholder.typicode.com/posts",
new Response.Listener<String>()
{
@Override
public void onResponse(String s)
{
Log.d("MYLOG","The String: "+s);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError volleyError)
{
Log.d("MYLOG",volleyError.toString());
}
});
mQueue.add(getRequest);
// JSONObject Request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
"https://jsonplaceholder.typicode.com/posts",null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Log.d("MYLOG",response.toString());
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.d("MYLOG",error.toString());
}
});
mQueue.add(jsonObjectRequest);
Post
StringRequest questpost = new StringRequest(Request.Method.POST,
"http://jsonplaceholder.typicode.com/posts", new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d("MYLOG","String: "+response);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error)
{
Log.d("MYLOG","ERROR: "+error.toString());
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String,String> map = new HashMap<>();
map.put("title","foo");
map.put("body","bar");
map.put("userId","444");
return map;
}
};
mQueue.add(questpost);
PUT
StringRequest questpost = new StringRequest(Request.Method.PUT,
"https://jsonplaceholder.typicode.com/posts/1", new Response.Listener<String>(
{
@Override
public void onResponse(String response)
{
Log.d("MYLOG","String: "+response);
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.d("MYLOG","ERROR: "+error.toString());
}
})
{
// Add Header
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String,String> map = new HashMap<>();
map.put("Content-Type","application/x-www-form-urlencoded");
return map;
}
// Add Params
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String,String> map = new HashMap<>();
map.put("title","SSSSSSSfoo");
return map;
}
};
mQueue.add(questpost);