无法将字符串转换为InputStream来解析XML
问题描述:
我想解析来自服务器的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;
}
}
}
}
答
这里没有解决的问题。 [email protected]
就是ByteArrayInputStream.toString()
的返回值。
但是你为什么要这样做?将整个URL加载到内存中会增加延迟并浪费空间,并且不适合超出特定大小。没有好处。只需返回con.getInputStream().
试过sb.toString()? – Jeyaprakash
你为什么这样做?将整个URL加载到内存中会增加延迟并浪费空间,并且不适合超出特定大小。没有好处。只需'返回con.getInputStream()'。 – EJP