如何解决android rest client不支持的媒体类型
| 我尝试从android模拟器向静态服务器发送请求。但是我总是得到错误: 415不支持的媒体类型。 客户代码:public JSONtest() throws Exception,IOException{
HttpPost request = new HttpPost(AppServerIP);
JSONObject param = new JSONObject();
param.put(\"name\",\"weiping\");
param.put(\"password\",\"123456\");
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
String retSrc = EntityUtils.toString(httpResponse.getEntity());
System.out.println(httpResponse.getStatusLine().getReasonPhrase());
}
服务器代码:
public class resource {
@POST
@Path(\"/trigger\")
@Consumes(MediaType.APPLICATION_JSON)
public Response trigger(JSONObject notify) throws Exception{
return Response.status(Response.Status.OK).entity(\"134124\").tag(\"213q\").type(MediaType.APPLICATION_JSON).build();
}
解决方法
问题在于服务器不知道客户端请求的媒体类型。
在客户端代码中尝试以下操作:
request.setHeader(\"Content-Type\",\"application/json\");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。