字符串无法在Android应用程序中保存完整的HttpResponse

时间:2014-07-08 10:24:43

标签: android string parsing httpresponse xmlpullparser

我从Web服务获得了非常大的响应。我将该内容保存在字符串中以将其传递给XmlPullParser,但该字符串无法完全保留它,因此我得到关于END TAG的例外。

有没有办法直接或以其他方式传递响应? (我不能分开回复)

获得以下例外:

    org.xmlpull.v1.XmlPullParserException: END_TAG expected (position:START_TAG <ItemCountUnit {http://www.w3.org/2001/XMLSchema-instance}xsi:type='ebl:UnitOfMeasure'>@1:5805 in java.io.StringReader@43f70e90) 
    07-09 10:04:04.064 W/System.err(19071):     at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:2077)
    07-09 10:04:04.064 W/System.err(19071):     at com.my.project.parsers.MyParser.ParseData(MyParser.java:307)
    07-09 10:04:04.064 W/System.err(19071):     at com.my.project.network.NetworkTask.doInBackground(NetworkTask.java:144)
    07-09 10:04:04.064 W/System.err(19071):     at com.my.project.network.NetworkTask.doInBackground(NetworkTask.java:1)
    07-09 10:04:04.064 W/System.err(19071):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    07-09 10:04:04.064 W/System.err(19071):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    07-09 10:04:04.064 W/System.err(19071):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    07-09 10:04:04.064 W/System.err(19071):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    07-09 10:04:04.074 W/System.err(19071):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    07-09 10:04:04.074 W/System.err(19071):     at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

使用此代码

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append((line + "\n"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}