2015-12-23 46 views
1

我有一个字符串在网页上回显。出于某种原因,我得到很多添加的字符或有时完整的字符串不会进来。字符串是:如何在输入流中读取严格可读的字符

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

为什么不尝试使用排气或改造来获得响应? AFAIK他们比这更可靠和快速 –

回答

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

对于快速参考解析,如果产生的字串有一个根前缀,只需启动一个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 
} 
+0

工程就像一个魅力。字符串构建正是我所需要的。谢谢 – Doopler

+0

不客气! – KoalaKoalified

0

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