从URL流分析XML似乎是一个可怕的想法。为什么不完全获取流,然后解析它呢?
public static String doGet(String url)throws ClientProtocolException, IOException{
HttpGet getRequest = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getRequest);
return responseToString(response);
private static String responseToString(HttpResponse httpResponse)
throws IllegalStateException, IOException{
StringBuilder response = new StringBuilder();
String aLine = new String();
//InputStream to String conversion
InputStream is = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while( (aLine = reader.readLine()) != null){
response.append(aLine);
}
reader.close();
return response.toString();
}
string serverInterestURI = 'www.test.com/whatever.php?something=what&other=lolwut'
String response = ServerHttpRequest.doGet(serverInterestURI);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = null;
XMLReader xr = null;
MatchedUsersParser myXmlHandler = null;
try {
sp = spf.newSAXParser();
// Get the XMLReader of the SAXParser we created.
xr = sp.getXMLReader();
// Create a new ContentHandler and apply it to the XML-Reader
myXmlHandler = new MatchedUsersParser();
xr.setContentHandler(myXmlHandler);
// Parse the xml-data from the server response.
xr.parse(new InputSource(new StringReader(response)));
// Parsing has finished.
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}