-1

I am working on a Server-Client application. For part of the requests, I need to generate a piece of Json String on Server side and send it to Client. The Json String was generated correctly on the server side according to the Server log. But on the Android client side, the String was changed and cannot be parsed as correct Json String.

Here are some related code.

Generate Json String on Server side:

@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();
}

This generates following Json code:

[{"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}]

And receive it on Android client:

......
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();

The response variable was written with value:

[{"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,"

Which cannot be parsed as Json String correctly with obvious errors.

Most methods which return a Json String to client request work fine as I expected except this one. But this method was implemented almost exactly the same as those ones work correctly. Thus I have no idea how this happened at all. Any one got any clew please help.

Alex.Y
  • 89
  • 7

1 Answers1

0

You're building String the wrong way.

Try this instead:

    // …

    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();

    // …
Hadi Satrio
  • 428
  • 2
  • 8
  • `bis.read()` even with buffered input it is slow(when you compare to reading with some buffer) – Selvin Oct 10 '16 at 08:52
  • This helps, thank you Hadi. But seems Selvin is right, the BufferedInputStream.read() method reads only one byte per each invoking, and that is slow. Do we have a better way other than this? – Alex.Y Oct 10 '16 at 09:16