【问题标题】:unsupported media type for an android rest clientandroid rest 客户端不支持的媒体类型
【发布时间】:2012-02-24 18:43:21
【问题描述】:

我尝试从 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();       
}

【问题讨论】:

  • 哈,这正是我面临的问题!非常感谢

标签: android rest types media


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2015-10-25
    • 2012-04-19
    • 1970-01-01
    • 2013-02-01
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多