【发布时间】:2013-12-12 21:34:28
【问题描述】:
场景:将 json 对象中的用户名和密码传递给 restful webservice,并获得一个 json 对象作为回报。是的,我知道,它很简单,但我无法让它工作。
我这几天一直在尝试。到目前为止,我已经尝试过:
我的宁静网络服务代码
@POST
@Path("/testJson")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject testJson(JSONObject inputJsonObj) throws Exception {
JSONObject myjson = new JSONObject();
if(inputJsonObj != null){
System.out.println("=================================");
System.out.println("JSON object = " + inputJsonObj.toString());
System.out.println("=================================");
}
else{
System.out.println("JSON is NULL");
}
myjson.put("success", "1");
System.out.println(myjson.toString());
// return "string returned";
return myjson;
}
在我的 android 活动中,代码是
// POST request to <service>/SaveVehicle
HttpPost request = new HttpPost(myURL);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("user-agent", "Yoda");
try {
// Build JSON string
// JSONStringer vehicle = new JSONStringer().object()
// .key("getItInputTO").object().key("zipCode").value("90505")
// .key("financingOption").value("B").key("make")
// .value("Scion").key("baseAmountFinanced").value("12000")
// .key("modelYear").value("2010").key("trimCode")
// .value("6221").key("totalMSRP").value("15000")
// .key("aprRate").value("").endObject().endObject();
JSONObject myjson = new JSONObject();
myjson.put("1", "first");
myjson.put("2", "second");
StringEntity entity = new StringEntity(myjson.toString());
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8"));
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
// HttpResponse response = httpClient.execute(request, localContext);
HttpResponse response = httpClient.execute(request);
resCode = response.getStatusLine().getStatusCode();
Toast.makeText(getApplicationContext(),
response.getStatusLine().getStatusCode() + "",
Toast.LENGTH_LONG).show();
if (resCode == 200) {
Toast.makeText(getApplicationContext(),
response.getStatusLine().getStatusCode() + "",
Toast.LENGTH_LONG).show();
HttpEntity entity2 = (HttpEntity) response.getEntity().getContent();
String text = getASCIIContentFromEntity(entity);
if(text!=null){
JSONObject obj = new JSONObject(text);
lblMsg.setText("Successful!");
}
// BufferedReader in = new BufferedReader(new
// InputStreamReader(response.getEntity().getContent()));
//
//
// String line = "";
// StringBuffer returnFromServer = new StringBuffer();
//
// while ((line = in.readLine()) != null) {
// returnFromServer.append(line);
// }
// // Toast what we got from server
// Toast.makeText(getApplicationContext(),
// returnFromServer.toString(), Toast.LENGTH_LONG).show();
//
// if (entity != null) {
// entity.consumeContent();
// }
}
} catch (Exception e) {
// TODO: handle exception
}
评论部分显示以前的尝试。
我在服务器控制台上获得的输出
=================================
JSON object = {}
=================================
{"success":"1"}
我的服务器端接收器 json 对象没有被填充,我不知道为什么。
注意:
- 我的 android 清单中有 INTERNET 和许多其他权限。
- 我的网络服务已启动并正在运行。
- 我拥有所有必需的 jar,即 jersey、json 等
- 我正在使用 Tomcat 7 来提供 restful web 服务
我将非常感谢任何帮助。 谢谢
【问题讨论】:
-
听起来这可能是服务器端的问题。预期的结果是什么?您是否在应用之外测试过服务器响应?
-
预期的结果是我在服务器端得到了传递过来的json对象。这是我传递的 JSONObject myjson = new JSONObject(); myjson.put("1", "first"); myjson.put("2", "second");
-
没人回答吗?
标签: java android json web-services rest