我知道如何使用的WebView加载在Android的网站的内容(webview.loadUrl(“http://slashdot.org/”);)加载数据
我怎样才能把网站在字符串变量的内容(我想以后解析这个字符串,XML,但这是未来一点改进)
我知道如何使用的WebView加载在Android的网站的内容(webview.loadUrl(“http://slashdot.org/”);)加载数据
我怎样才能把网站在字符串变量的内容(我想以后解析这个字符串,XML,但这是未来一点改进)
这是一个务实的回答你的问题:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.spartanjava.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
// Now you have the whole HTML loaded on the result variable
http://www.spartanjava.com/2009/get-a-web-page-programatically-from-android/
当然,测试响应的getStatusLine对于潜在的错误并不会有什么坏处。另外,我发现连接可能会中断而不会引发任何异常(如果发生InterruptedIOException可能会发生),因此检查长度或校验和也是好的 – njzk2
而不是使用WebView您可以使用HTTPClient API并执行GET请求。然后你会得到一个你可以用来获取字符串的InputStream。 – ZeissS