Java 在服务器端正确生成Json字符串时,客户端接收到错误的Json字符串
我正在开发一个服务器客户端应用程序。对于部分请求,我需要在服务器端生成一段Json字符串并将其发送给客户端。根据服务器日志,在服务器端正确生成了Json字符串。但在Android客户端,字符串已更改,无法解析为正确的Json字符串 下面是一些相关代码 在服务器端生成Json字符串:Java 在服务器端正确生成Json字符串时,客户端接收到错误的Json字符串,java,android,json,spring,Java,Android,Json,Spring,我正在开发一个服务器客户端应用程序。对于部分请求,我需要在服务器端生成一段Json字符串并将其发送给客户端。根据服务器日志,在服务器端正确生成了Json字符串。但在Android客户端,字符串已更改,无法解析为正确的Json字符串 下面是一些相关代码 在服务器端生成Json字符串: @RequestMapping(value="/resourceList/{actType}/{type}/{id}") @ResponseBody public Object personList(@PathVar
@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();
}
并在Android客户端上接收:
......
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字符串,并出现明显错误
除此之外,大多数向客户机请求返回Json字符串的方法都可以正常工作。但这种方法的实现几乎与那些正确工作的方法完全相同。因此,我完全不知道这是怎么发生的。任何人都有任何提示,请帮助。您正在以错误的方式构建
字符串
请尝试以下方法:
// …
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();
// …
如果len
小于buffer
size怎么办?这就是为什么我必须调用String.trim()方法。做一些研究bis.read()
即使使用缓冲输入,速度也很慢(与使用缓冲区的读取相比),这很有帮助,谢谢Hadi。但是似乎Selvin是对的,BufferedInputStream.read()方法每次调用只读取一个字节,这很慢。除了这个,我们还有更好的办法吗?
// …
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();
// …