android rest客户端不支持的媒体类型

前端之家收集整理的这篇文章主要介绍了android rest客户端不支持的媒体类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试从 android模拟器发送请求到一个安静的服务器.但我总是得到错误

415 Unsupported Media Type.

客户代码

  1. public JSONtest() throws Exception,IOException{
  2.  
  3. HttpPost request = new HttpPost(AppServerIP);
  4. JSONObject param = new JSONObject();
  5. param.put("name","weiping");
  6. param.put("password","123456");
  7. StringEntity se = new StringEntity(param.toString());
  8. request.setEntity(se);
  9. HttpResponse httpResponse = new DefaultHttpClient().execute(request);
  10. String retSrc = EntityUtils.toString(httpResponse.getEntity());
  11. System.out.println(httpResponse.getStatusLine().getReasonPhrase());
  12. }

服务器的代码

  1. public class resource {
  2. @POST
  3. @Path("/trigger")
  4. @Consumes(MediaType.APPLICATION_JSON)
  5. public Response trigger(JSONObject notify) throws Exception{
  6. return Response.status(Response.Status.OK).entity("134124").tag("213q").type(MediaType.APPLICATION_JSON).build();
  7. }

解决方法

问题是服务器不知道客户端请求的媒体类型.
在客户端代码中尝试这样的事情:

request.setHeader(“Content-Type”,“application / json”);

猜你在找的Android相关文章