2011-05-08 28 views
1

我想获取一些XML并最终将其用作字符串。这是我的两种方法,前者称为后者。困惑。我怎样才能抓住返回的InputStream?

public static void getAllXML(String url) throws XmlPullParserException, IOException, URISyntaxException{ 
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(true); 

    XmlPullParser parser = factory.newPullParser(); 
    parser.setInput(new InputStreamReader(getUrlData(url))); 

    XmlUtils.beginDocument(parser,"results"); 

    int eventType = parser.getEventType(); 
    do{ 
     XmlUtils.nextElement(parser); 
     parser.next(); 
     eventType = parser.getEventType(); 
     if(eventType == XmlPullParser.TEXT){ 
      Log.d("test",parser.getText()); 
     } 
    } while (eventType != XmlPullParser.END_DOCUMENT) ;  

} 

public static InputStream getUrlData(String url) throws URISyntaxException, ClientProtocolException, IOException { 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpGet method = new HttpGet(new URI(url)); 
    HttpResponse res = client.execute(method); 
    return res.getEntity().getContent(); 

} 

然后我使用它接受一个InputStream并将其转换成一个String的方法。但是,我怎样才能抓住getUrlData方法返回的InputStream,所以我可以拨打convertStreamToString(myInputStream)

+0

假设你发布的代码编译并运行良好,你能够做到这一点:InputStream myInputStream = getUrlData(url); convertStreamToString(myInputStream); ? – 2011-05-08 17:06:27

回答

0

可以使用TeeInputStreamhttp://commons.apache.org/io/api-1.4/org/apache/commons/io/input/TeeInputStream.html)来获取你传递给解析器,也写输入一个ByteArrayInputStream,你可以再与ByteArrayOutputStream.toString(String encoding)使用的输入流。

喜欢的东西

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
TeeInputStream is = new TeeInputStream(getDataUrl(...), bytes); 
try { 
    // Pass is to parser here. 
    parser.setInput(new InputStreamReader(is, "UTF-8")); 
    // Note: please specify an encoding with InputStreamReader, maybe based on 
    // the headers from getDataUrl. 
    ... 
} finally { 
    is.close(); 
} 

// After parsing finishes, convert the bytes to a String. 
String parsed = bytes.toString("UTF-8"); 
// If UTF-8 is not appropriate, use the encoding specified in headers from URL. 

或者,也可以在第一读取字符串,然后用StringReaderparser.setInput。如果您的解析器设置为拒绝大型输入,或者可以采用InputStream作为输入,并执行稍后可能要使用的字符集检测,则上述方法更加灵活。

0

只需使用java.io API读取流?

喜欢的东西:

package essai; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

public class Reader { 

    public static String convertStreamToString (InputStream stream) throws IOException { 
    String result = ""; 

    //Wrap the input stream into a buffered reader so reading is faster and more important, easier with the use of readLine method. 
    InputStreamReader reader = new InputStreamReader(stream); 
    BufferedReader bufferedReader = new BufferedReader(reader); 

    String currentLine = bufferedReader.readLine(); 
    while (currentLine !=null) { 
     result+=currentLine; 
     currentLine = bufferedReader.readLine(); 
    } 
    return result; 
    } 

} 

理所当然的,你可以优化,如果你的XML是巨大的,没有回车尤其是常见的错误是,但它应该让你开始。