![](/img/trans.png)
[英]org.apache.http.client.HttpResponseException: Bad Request - Sending JSON request via REST
[英]Error : org.apache.http.client.HttpResponseException: Unsupported Media Type
我正在尝试使用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());
}
});
提前致谢。
如果您正在获取org.apache.http.client.HttpResponseException: Unsupported Media Type
应用程序中org.apache.http.client.HttpResponseException: Unsupported Media Type
,而不是REST客户端中org.apache.http.client.HttpResponseException: Unsupported Media Type
,则问题可能与您在POST活动中添加其他内容有关。 服务器可能接收到太多(或可能太少)参数,因此发送回错误响应。
在没有看到您的实际帖子的情况下,我无法确切查明问题出在哪里,但我的建议是尝试查看服务器从POST接收的内容,并确保您在Android POST中发送的项目与REST API客户端匹配完全发布。
就我而言,问题在于我没有设置HTTP标头之一,因此我必须添加:
.setHeader(HttpHeaders.CONTENT_TYPE,"multipart/form-data")
或您的内容类型是什么...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.