Williams Williams - 4 months ago 36
Java Question

Error : org.apache.http.client.HttpResponseException: Unsupported Media Type

I am trying to call web api using http://loopj.com/android-async-http/ & using

POST
method. I am sending json & content type in my code that's always gives error message.


org.apache.http.client.HttpResponseException: Unsupported Media Type


When I test same in the rest client using same json & content type its works fine. I don't know what more I need to set to execute it correctly.

Please see my below code.

// creating JSON using GSON library
Login mLogin = new Login();
mLogin.setUserName(userName);
mLogin.setPassword(password);

Gson gson = new GsonBuilder().create();
String loginJSON = gson.toJson(mLogin);

mHttpEntity = new StringEntity(loginJSON);

// execute the http asynchronously using post http method
asyncHttpClient.post(getActivity(), URL, mHttpEntity, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Logger.d(TAG, responseBody.toString());
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Logger.d(TAG, responseBody.toString());
}
});


Thanks in advance.

Answer

If you're getting, org.apache.http.client.HttpResponseException: Unsupported Media Type in your app but not in a REST client, then the problem likely has to do with you putting extra things in the POST activity. The server is probably receiving too many (or possibly too few) parameters and therefore is sending back a bad response.

Without seeing your actual post, I can't pinpoint exactly where the problem is, but my suggestion is to try to see what the server is receiving from your POST and make sure that the items you send in the Android POST match the REST API Client POST exactly.

Comments