loading data from site as String(Android)

前端 未结 1 1902
不思量自难忘°
不思量自难忘° 2021-01-28 03:28

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

相关标签:
1条回答
  • 2021-01-28 04:17

    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/

    0 讨论(0)
提交回复
热议问题