0

I know how to load site's content in Android using WebView (webview.loadUrl("http://slashdot.org/");)

And how can I put site's content in String variable(After I'd like to parse this string to XML but this is next problen)

takayoshi
  • 2,789
  • 5
  • 36
  • 56
  • Instead of using the WebView you could use the HTTPClient API and perform a GET request. Then you will get an InputStream which you could consume to get the String. – ZeissS Aug 19 '11 at 07:14

1 Answers1

1

Here is a pragmatical answer to your question:

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/

GrandMarquis
  • 1,913
  • 1
  • 18
  • 30
  • 1
    Of course, it wouldn't hurt to test the getStatusLine of the response for a potential error. Also, I found that the connection may be interrupted without any exception raised (where an InterruptedIOException could have been expected), so checking for the length or checksum is good too – njzk2 Aug 19 '11 at 09:00