【发布时间】: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