Reputation: 4661
This is the first time I'm working with JSON but I'm experimenting with POST requests.
I want to make a POST request to this website http://jsonblob.com/api
My code works when I use an asynctask + httppost
private static final String url = "http://jsonblob.com/api/jsonBlob";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new PostJsonTask().execute();
}
private class PostJsonTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
HttpResponse response = post();
Header s = response.getFirstHeader("Location");
Log.d("PostJsonTask", s.getValue());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private HttpResponse post() throws Exception {
// convert parameters into JSON object
Map<String, String> params = new HashMap<String, String>();
params.put("user", "Robby");
params.put("pass", "zqdqzdqdqzd");
JSONObject holder = new JSONObject(params);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(holder.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
post.setEntity(se);
return client.execute(post);
}
}
With this code I can succesfully get the Location parameter.
But I want to do this with the Volley library, I already have the following code :
private static final String url = "http://jsonblob.com/api/jsonBlob";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, url, createResponseListener(), createErrorListener()) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Content-type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("user", "Robby");
params.put("pass", "zqdqzdqdqzd");
return params;
}
@Override
protected Response<String> parseNetworkResponse(
NetworkResponse response) {
Map<String, String> headers = response.headers;
String location = headers.get("Location");
Log.d("PostJsonTask", location);
return super.parseNetworkResponse(response);
}
};
queue.add(request);
}
private ErrorListener createErrorListener() {
return new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
};
}
private Listener<String> createResponseListener() {
return new Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("PostJsonTask", response);
}
};
}
But this returns a VolleyError with code 400. How can I fix this?
Upvotes: 4
Views: 5950
Reputation: 91
Instead of trying to set Content-type of header try to set for bodyContentType and in postman(web) also you are setting the content type of body only
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.toString());
}
})
@Override
public String getBodyContentType() {
return "application/json";
}
};
Upvotes: 2
Reputation: 8700
Don't use the getParams
override.
Instead, create a JSON with these parameter and provide as the body of the request like they do in JsonObjectRequest
(or just use a JsonObjectRequest
instead of a StringRequest
), e.g.
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("user", "Robby");
jsonObject.put("pass", "zqdqzdqdqzd");
} catch (JSONException e) {
// handle exception (not supposed to happen)
}
StringRequest request = new StringRequest(Request.Method.POST, url, createResponseListener(), createErrorListener()) {
// your code without getParams
@Override
public byte[] getBody() {
try {
return jsonObject.toString.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
// not supposed to happen
return null;
}
}
}
Upvotes: 1