如何使用输入流读取严格可读的字符

时间:2015-12-23 21:45:40

标签: java android inputstream

我在网页上回显了一个字符串。出于某种原因,我收到了很多添加的字符,或者有时完整的字符串没有进来。字符串是:

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

我在字符串中阅读的有趣部分是:

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;
}

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

但我有时会在没有完整字符串的情况下收到此响应,这会导致我的代码无法正常工作:

        {"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",                 ���������������������������������������������...

2 个答案:

答案 0 :(得分:0)

如果编码不是真正的UTF-8,你会遇到这样的问题。您应输出http响应标头,该标头还应提及它使用UTF-8编码发送信息。

答案 1 :(得分:0)

您可以尝试这样的事情:

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

然后通过实例化JSONArray

进行解析

为了快速参考,如果生成的String具有根前缀,则只需启动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
}