我知道如何使用的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