Skip to content

Instantly share code, notes, and snippets.

@omegachien
Created April 10, 2015 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegachien/1ed90db2d442cc38c5d5 to your computer and use it in GitHub Desktop.
Save omegachien/1ed90db2d442cc38c5d5 to your computer and use it in GitHub Desktop.
Volley POST v1.1
HashMap<String, String> params = new HashMap<String, String>();
float price = 15.0f;
int userId = 1;
String url = "greenroom.com.my/api/v1/price/update";
//Construct signature and POST param
params.put("price", price);
params.put("user_id", userId);
Log.d("URL", url);
JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(
params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//success, maybe show message telling user?
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
if (response != null && response.data != null) {
switch (response.statusCode) {
case 400:
json = new String(response.data);
try {
JSONObject obj = new JSONObject(json);
JSONObject objError = obj.getJSONObject("error");
String message = objError.getString("message");
Log.d("ERROR VOLLEY", message);
} catch (Exception e) {
}
break;
}
}
}
});
// add the request object to the queue to be executed
sma.addToRequestQueue(req);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment