I am trying to post json object
{"venue":"places, US"}
using below code-
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 30000);
HttpResponse response;
try {
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
response = client.execute(post);
} catch (Exception e) {
Log.e("Client", "sendJson", e);
}
However I am getting below error response from server-
{"description":"Invalid token character ',' in token \"json, application/json\"","errorCode":2000}
What is reason for that error from from server. Is is because of some header setting on server side or there is some problem with request code I pasted above.
Edit:
Looks like this error is due to content type setting 2 times. After removing below line
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
error is gone.
But when I only remove below line instead of line written above, it does not remove error.
post.setHeader("Content-type", "application/json");
Why removing only first line works and when I remove 2nd one error is still there as both are setting content type.
Also I have used that code with both lines in it in other projects and it worked in them without any error. It looks like server side issue, what setting on server may have caused this problem.
Thanks
Try removing the
line. It looks like you’r adding the content-type header twice and the server has some problems with that.