开发者

Confused. How can I grab hold of the returned InputStream?

开发者 https://www.devze.com 2023-03-03 23:07 出处:网络
I\'m trying to fetch some XML and eventually use it as a String. Here\'s my two methods, the former of which calls the latter.

I'm trying to fetch some XML and eventually use it as a String. Here's my two methods, the former of which calls the latter.

public static void getAllXML(String url) throws XmlPullParserException, IOException, URISyntaxException{ 
    XmlPullParserFactory factory = XmlPullPa开发者_StackOverflowrserFactory.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();

}

Then I use a method which takes an InputStream and converts it to a String. But how do I grab hold of the InputStream returned by the getUrlData method so I can call convertStreamToString(myInputStream)?


You can use a TeeInputStream ( http://commons.apache.org/io/api-1.4/org/apache/commons/io/input/TeeInputStream.html ) to get the input stream that you pass to a parser and also write the input to a ByteArrayInputStream that you can then use with ByteArrayOutputStream.toString(String encoding).

Something like

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.

Alternatively, you can read the string in first, and then use a StringReader with parser.setInput. If your parser is set up to reject large inputs, or can take an InputStream as input and does charset detection that you might want to use later, then the approach above is more flexible.


Just read the stream with java.io API?

Something like :

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;
  }

}

Of course a you could optimize and a common error in particular would be if your XML is huge with no carriage return, but it should get you started.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号