【问题标题】:Incorrect Json String was received on client while it was generated correctly on server side在服务器端正确生成时,客户端收到了不正确的 Json 字符串
【发布时间】:2016-10-10 08:31:11
【问题描述】:

我正在开发一个服务器-客户端应用程序。对于部分请求,我需要在Server端生成一段Json String并发送给Client。根据服务器日志,在服务器端正确生成了 Json 字符串。但是在Android客户端,String被改变了,无法解析为正确的Json String。

这里有一些相关代码。

在服务器端生成 Json 字符串:

@RequestMapping(value="/resourceList/{actType}/{type}/{id}")
@ResponseBody public Object personList(@PathVariable int actType, @PathVariable int type, @PathVariable int id){
    ArrayList<ItemBase> list = new ArrayList(); 
    ......
    return new ArrayList();
}

这会生成以下 Json 代码:

[{"countryID":1,"locationID":5,"siteID":5,"brief":"shark point","userID":0,"status":"normal","rank":0.0,"id":2,"timestamp":1471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"displayName":"halik","pic":"3.jpg","actType":1,"type":64}]

并在安卓客户端接收:

......
HttpURLConnection urlConnection = null;
try {
    URL url = new URL(request);
    urlConnection = (HttpURLConnection) url.openConnection();
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    byte[] buffer = new byte[256];
    int len = 0;
    StringBuilder responseBuilder = new StringBuilder();
    while ((len = in.read(buffer)) != -1) {
        String str = new String(buffer, "utf-8");
        responseBuilder.append(str);
    }
    String response = responseBuilder.toString().trim();

响应变量被写入值:

[{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"displayName":"halik","pic":"3.jpg","actType":1,471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,""type":64}]":"halik","pic":"3.jpg","actType":1,471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"

无法正确解析为 Json String 且有明显错误。

除了这个之外,大多数向客户端请求返回 Json 字符串的方法都可以正常工作。但是这种方法的实现几乎与那些正常工作的方法完全相同。因此,我根本不知道这是怎么发生的。谁有任何线索请帮忙。

【问题讨论】:

标签: java android json spring


【解决方案1】:

你构建 String 的方式错误。

试试这个:

    // …

    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(request);
        urlConnection = (HttpURLConnection) url.openConnection();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        int result = bis.read();
        while(result != -1) {
            buf.write((byte) result);
            result = bis.read();
        }

        String response = buf.toString();

    // …

【讨论】:

  • bis.read() 即使使用缓冲输入,它也很慢(与使用某些缓冲区读取相比)
  • 这有帮助,谢谢哈迪。但似乎 Selvin 是对的,BufferedInputStream.read() 方法每次调用只读取一个字节,这很慢。除了这个,我们还有更好的方法吗?
猜你喜欢
  • 2012-04-08
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 2011-06-07
  • 2012-01-26
  • 2022-09-28
  • 1970-01-01
相关资源
最近更新 更多