【问题标题】:A message body reader for Java type, class org.json.JSONObject.... and MIME media type, application/json was not found未找到 Java 类型、类 org.json.JSONObject.... 和 MIME 媒体类型、application/json 的消息正文阅读器
【发布时间】:2014-03-20 20:53:28
【问题描述】:

我正在尝试从 android 调用 jersey restful web 服务。我的安卓代码是

客户端代码:

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://X.X.X.X:8080/RestfulService/rest/post");
post.setHeader("content-type", "application/json");

JSONObject dato = new JSONObject();
dato.put("email", email);
dato.put("password", password);

StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String rs = EntityUtils.toString(resp.getEntity());
return rs

网络服务代码

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL(JSONObject json) {

String password = (String) json.get("password");
String email = (String) json.get("email");

*I am using the string values to get the result from the database*

}

我得到的错误类似于 com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, class org.json.JSONObject.... and MIME media type, application/未找到 json。

非常感谢您的帮助

【问题讨论】:

  • 您能列出附加到您的服务项目的库文件吗?
  • @Joshi 我已经包含了所有的 jersey 1.18 jar 文件
  • 您得到的是 415 Unsupported Media Type,不是吗?我也为此苦苦挣扎。 Tomcat,或者你后端的任何东西,除了字符串什么都不知道。如果您将 JSONObject 更改为 String,您会发现您实际上进入了 AuthMySQL() 方法,其中 json 只是作为字符串,但是您必须使其可解析,以便您可以使用您编写的代码或设置一些东西Jersey 可以用来将传入的 JSON 字符串转换为 JSONObject。这就是我卡住的地方。 (不知道这是否有帮助或为时已晚。)

标签: java android json web-services rest


【解决方案1】:

当您没有包含正确的库以将 json 正确映射到 POJO 或没有合适的 POJO 用于输入时,就会发生这种情况。

看看将jersey-json maven dependency 添加到您的项目中

【讨论】:

    【解决方案2】:

    如果您不想添加库,而只想获取解析后的 JSON(即不映射到 POJO),那么您只需实现一个基本的MessageBodyReader,例如:

    public class JSONObjectMessageBodyReader implements MessageBodyReader<JSONObject> {
        @Override
        public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return type == JSONObject.class && mediaType.equals(MediaType.APPLICATION_JSON_TYPE);
        }
    
        @Override
        public JSONObject readFrom(Class<JSONObject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            try {
                // Using Apache Commons IO:
                String body = IOUtils.toString(entityStream, "UTF-8");
                return new JSONObject(body);
            } catch(JSONException e) {
                throw new BadRequestException("Invalid JSON", e);
            }
        }
    }
    

    然后在您的网络服务代码中:

    @POST
    public Response doSomething(JSONObject body) {
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 2016-06-14
      • 2012-08-16
      • 2016-10-01
      • 2013-12-15
      • 2018-04-20
      • 1970-01-01
      • 2016-04-03
      • 2011-12-11
      相关资源
      最近更新 更多