2017-08-02 123 views
-1

我想解析來自服務器的XML代碼以便在Android中使用。該URL正在工作,直到SB我得到XML。將String轉換爲InputStream時,我在logcat中得到了這個:[email protected] 有什麼幫助? 謝謝!無法將字符串轉換爲InputStream來解析XML

private InputStream downloadUrl(String urlString) throws IOException { 
BufferedReader reader = null; 

try { 
    URL url = new URL(urlString); 
    HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
    con.setConnectTimeout(60000); 
    con.setReadTimeout(60000); 
    StringBuilder sb = new StringBuilder(); 
    reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

    String line; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 

    InputStream stream = IOUtils.toInputStream(sb, "UTF-8"); 
    Log.d(TAG, "SB " + sb); 
    Log.d(TAG, "STREAM" + stream); 
    return stream; 
} catch (Exception e) { 
    e.printStackTrace(); 
    return null; 
} finally { 
    if (reader != null) { 
     try { 
      reader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
} 

}

+0

試過sb.toString()? – Jeyaprakash

+1

你爲什麼這樣做?將整個URL加載到內存中會增加延遲並浪費空間,並且不適合超出特定大小。沒有好處。只需'返回con.getInputStream()'。 – EJP

回答

0

這裏沒有解決的問題。 [email protected]就是ByteArrayInputStream.toString()的返回值。

但是你爲什麼要這樣做?將整個URL加載到內存中會增加延遲並浪費空間,並且不適合超出特定大小。沒有好處。只需返回con.getInputStream().

相關問題