简体   繁体   中英

How to read in strictly readable characters with input stream

I have a string echoed on a webpage.For some reason I am getting a lot of added characters or sometimes the full string doesn't come in. The string is:

        {"restaurant0": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant1": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant2": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant3": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant4": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant5": {
                "name":" Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718", "latitude":"-79.789334", 
                "county":"345 North Elm Street Austin, TX 27401", 
                "description":"Test" },
                "rows": {"row": "6"}}

The interesting part to how I am reading in the string is here:

private String loadFromNetwork(String urlString) throws IOException {
    InputStream stream = null;
    String str = "";

    try {
        stream = downloadUrl(urlString);
        str = readIt(stream, 65535);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
    return str;
}

AND

private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}

But I am sometimes receiving this response without the full string, which causes my code to not work correctly:

        {"restaurant0": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant1": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant2": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant3": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant4": {
                "name":"Jakes",
                "deal_title":"Test",
                "image":"Test",
                "longitude":"36.067718",
                "latitude":"-79.789334",
                "county":"345 North Elm Street
Austin, TX 27401",
                "description":"Test"

                        },"restaurant5": {
                "name":" Jakes",
                "deal_title":"Test",
                "image":"Test",                 ���������������������������������������������...

If the encoding is not really UTF-8 you will have an issue like this. You should output the http response header, that header should also mentions that it sending the info using UTF-8 encoding.

You could try something like this:

protected String getJSONFromInputStream(InputStream is)
{
    if (is == null)
        throw new NullPointerException();
    //instantiates a reader with max size
    BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8 * 1024);

    StringBuilder sb = new StringBuilder();

    try
    {
        //reads the response line by line (and separates by a line-break)
        String line;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            //closes the inputStream
            is.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

And then parse through instantiating a JSONArray

For quick reference, if the resulting String has a root prefix, just initiate a JSONObject:

JSONObject json = new JSONObject(jsonAsString);
JSONArray jArray = json.getJSONArray("rootTag");
for (int i = 0; i < jArray.length(); i++)
{
    JSONObject currentJ = jArray.getJSONObject(i);
    //Do Something with the object
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM