Skip to content

Instantly share code, notes, and snippets.

@thedoapps
Last active March 6, 2016 13:36
Show Gist options
  • Save thedoapps/ab1bb202f036d750e5b3 to your computer and use it in GitHub Desktop.
Save thedoapps/ab1bb202f036d750e5b3 to your computer and use it in GitHub Desktop.
How to implement Web services consumption POST using "x-www-form-urlencode"
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST,
"github.com/api/login",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("LOGIN", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOGIN ERROR", error.toString());
}
})
{
@Override
public byte[] getBody() {
try
{
final String body = "&email=" + "test@test.com" +
"&password=" + "test";
return body.getBytes("utf-8");
}
catch (Exception ex) {
Log.e("getBodyEx", ex.toString());
}
return null;
}
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
return headers;
}
};
requestQueue.add(jsonObjectRequest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment