android文件解析InputStream问题解决

来源:互联网 发布:淘宝助理 未知错误 1 编辑:程序博客网 时间:2024/04/30 03:52


http://apps.hi.baidu.com/share/detail/17484624

org.xmlpull.v1.XmlPullParserException: Error parsing document. (position:line -1, column -1) caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 265: not well-formed (invalid token)在实际的开发中我们发送请求访问服务器端,当返回的是xml格式的InputStream对象时,有时我们采用解析方法如sax, pull 对流进行解析时会出现上面的情况,显示解析,这时可以将流对象读取到内存中转换成字符串,在以流的方式从内存中读出。重新解析就可以得到正确的集合对象

public static byte[] readInput(InputStream in ) throws IOException{
   ByteArrayOutputStream out=new ByteArrayOutputStream();
   int len=0;
   byte[] buffer=new byte[1024];
   while((len=in.read(buffer))>0){
    out.write(buffer,0,len);
   }
   out.close();
   in.close();
   return out.toByteArray();
}

public static InputStream getStringStream(String sInputString){
   ByteArrayInputStream tInputStringStream=null;
   if (sInputString != null && !sInputString.trim().equals("")){
   tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
   }
   return tInputStringStream;
   }


原创粉丝点击