【发布时间】:2014-11-11 19:57:24
【问题描述】:
我正在尝试使用 http://loopj.com/android-async-http/ 和使用 POST 方法调用 web api。我在我的代码中发送 json 和内容类型,总是给出错误消息。
org.apache.http.client.HttpResponseException: 不支持的媒体类型
当我在其他客户端使用相同的 json 和内容类型进行测试时,它工作正常。我不知道我还需要设置什么才能正确执行它。
请看我下面的代码。
// 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());
}
});
提前致谢。
【问题讨论】:
标签: java android json content-type android-async-http