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", "[email protected]");
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!
RequestParams requestParams = new RequestParams();
asyncHttpClient.addHeader("Accept", "text/json");
asyncHttpClient.addHeader("content-type", "application/json");
asyncHttpClient.post(context, getAbsoluteUrl(url), requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, String content) {
super.onSuccess(content
// Ur logic
}
@Override
public void onFailure(Throwable error, String content) {
super.onFailure(error, content);
// Ur logic
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With