Android Rest客户端不支持的媒体类型。

8
我尝试从Android模拟器发送请求到一个restful服务器,但是我总是收到错误:

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();       
}

哈哈,这就是我遇到的问题!非常感谢。 - woezelmann
1个回答

9

问题在于服务器不知道客户端请求的媒体类型。 在客户端代码中尝试这样做:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接