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

标签 android rest types media

我尝试从 android 模拟器向 restful 服务器发送请求。但我总是得到错误:

415 Unsupported Media Type.

客户端代码:

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");

关于android - android rest 客户端不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141789/

相关文章:

Java 泛型 : how to get a generic type from a method?

android - 自定义不带 PNG 的 ToggleButton

android - 无法从 Android 设备访问本地主机

java - 使用不同的线程自动更新 UI

ios - 带有错误映射的 RestKit PUT 请求

c++ - 将类型分配给原始文字的简写方式

android - Espresso - 在 ListView 中按文本点击

java - 使用 token (Java) 保护 REST Web 服务

php - 具有公共(public) RESTful API + 本地访问 API 的应用程序架构

python - 错误: incompatible types when assigning to type ‘__complex__ double *’ from type ‘complex double’