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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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); 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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()); } }) { @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; } @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("title","SSSSSSSfoo"); return map; } }; mQueue.add(questpost);
|