I'm using loopj's AsyncHttpClient for Android so that I can interact with a restful web application which I have created. I have tested a POST request using Postman and it works fine.
However, in Android I'm struggling to do a post request however as the content-type is always set as text/html..
RequestParams params = new RequestParams();
params.setUseJsonStreamer(true);
params.put("email", "android@tester.com");
StringEntity se = null;
try {
se = new StringEntity(params.toString());
se.setContentType("application/json");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Header headers[] = {};
if(getActivity() != null){
RestClient.postWithContentType(getActivity(), "contacts", se, "application/json", new AsyncHttpResponseHandler() {
//onSuccess and onFailure methods ommitted
});
It keeps failing, and I get this message in logcat: Passed contentType will be ignored because HttpEntity sets content type.
So, I attempted to change this,
public static void postWithContentType(Context context,String url,StringEntity s,String contentType, AsyncHttpResponseHandler responseHandler){
s.setContentType("application/json");
client.post(context, getAbsoluteUrl(url), s, contentType, responseHandler);
}
However I still get the same message, it's really frustrating, and I've been trying to figure it out for ages! If anyone has any idea about how to set the content type - it will be much appreciated,thanks!