How do you use the Android Volley API?
Edit: finally here it is an official training about "Volley library"
I found some examples about Volley library
6 examples by Ognyan Bankov :
- Simple request
- JSON request
- Gson request
- Image loading
- with newer external HttpClient (4.2.3)
- With Self-Signed SSL Certificate.
one good simple example by Paresh Mayani
other example by HARDIK TRIVEDI
- (NEW) Android working with Volley Library by Ravi Tamada
Hope this helps you
Unfortunately there is no documentation for a Volley library like JavaDocs until now. Only repo on github and several tutorials across the Internet. So the only good docs is source code :) . When I played with Volley I read this tutorial.
About post/get you can read this : Volley - POST/GET parameters
Hope this helps
This is an illustration for making a POST request using Volley. StringRequest is used to get response in the form of String.
Assuming your rest API returns a JSON. The JSON response from your API is received as String here, which you can covert again to JSON and process it further. Added comments in code.
StringRequest postRequest = new StringRequest(Request.Method.POST, "PUT_YOUR_REST_API_URL_HERE", new Response.Listener<String>() { @Override public void onResponse(String response) { try { final JSONObject jsonObject = new JSONObject(response); // Process your json here as required } catch (JSONException e) { // Handle json exception as needed } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { String json = null; NetworkResponse response = error.networkResponse; if(response != null && response.data != null){ switch(response.statusCode) { default: String value = null; try { // It is important to put UTF-8 to receive proper data else you will get byte[] parsing error. value = new String(response.data, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } json = trimMessage(value, "message"); // Use it for displaying error message to user break; } } loginError(json); progressDialog.dismiss(); error.printStackTrace(); } public String trimMessage(String json, String key){ String trimmedString = null; try{ JSONObject obj = new JSONObject(json); trimmedString = obj.getString(key); } catch(JSONException e){ e.printStackTrace(); return null; } return trimmedString; } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("abc", "pass abc"); params.put("xyz", "pass xyz"); // Pass more params as needed in your rest API // Example you may want to pass user input from EditText as a parameter // editText.getText().toString().trim() return params; } @Override public String getBodyContentType() { // This is where you specify the content type return "application/x-www-form-urlencoded; charset=UTF-8"; } }; // This adds the request to the request queue MySingleton.getInstance(YourActivity.this).addToRequestQueue(postRequest);
// Below is MySingleton class
public class MySingleton { private static MySingleton mInstance; private RequestQueue mRequestQueue; private static Context mCtx; private MySingleton(Context context) { mCtx = context; mRequestQueue = getRequestQueue(); } public static synchronized MySingleton getInstance(Context context) { if (mInstance == null) { mInstance = new MySingleton(context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { // getApplicationContext() is key, it keeps you from leaking the // Activity or BroadcastReceiver if someone passes one in. mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req) { getRequestQueue().add(req); }}