Joh*_*ohn 1 java json servlets httpresponse
我在我的java servlet中编写了如下JSON响应,其中JObject是创建的JSON对象
response.setContentType("application/json; charset=UTF-8");
PrintWriter printout = response.getWriter();
printout.print(JObject);
printout.flush();
Run Code Online (Sandbox Code Playgroud)
但它在接收方以文本/普通方式收到
[Server: Apache-Coyote/1.1, ETag: W/"XXXXXXXXXX", Last-Modified: Tue, 04 Jun 2013 10:42:31 GMT, Content-Type: text/plain, Content-Length: 2573, Date: Tue, 04 Jun 2013 10:44:01 GMT]
Run Code Online (Sandbox Code Playgroud)
如何获得确切的JSON响应?如果我JSON在同一台机器上编写响应,即可获取JSON数据.但是如果我JSON在另一台服务器上编写响应,它会返回text/plain.
这是JObject:
JSONObject JObject = new JSONObject();
JObject.put("Response", "1");
JObject.put("Message", "Client unauthorized");
Run Code Online (Sandbox Code Playgroud)
我不确定你在servlet中确切的代码是什么.但是我已经创建了一个示例Servlet,并使用相同的上述代码返回了Json输出.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/json; charset=UTF-8");
PrintWriter printout = response.getWriter();
JSONObject JObject = new JSONObject();
JObject.put("Response", "1");
JObject.put("Message", "Client unauthorized");
printout.print(JObject);
printout.flush();
// Or
// printout.write(JObject.toString());
}
Run Code Online (Sandbox Code Playgroud)
我{"Message":"Client unauthorized","Response":"1"}在浏览器上得到了输出.
这是拍摄的结果:

| 归档时间: |
|
| 查看次数: |
19328 次 |
| 最近记录: |