Get text from web page to string

前端 未结 3 1211
感情败类
感情败类 2020-11-27 05:46

I\'m new to Android and I want to get the whole text from a web page to a string. I found a lot of questions like this but as I said I\'m new to Android and I don\'t know ho

3条回答
  •  清酒与你
    2020-11-27 06:19

    Seeing as you aren't interested in Viewing the content at all, try using the following:

    In order to get your source code from an URL you can use this :

    HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
    HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
    HttpResponse response = httpclient.execute(httpget); // Executeit
    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); // Create an InputStream with the response
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) // Read line by line
        sb.append(line + "\n");
    
    String resString = sb.toString(); // Result is here
    
    is.close(); // Close the stream
    

    Make sure you run this off the main UI thread in an AsyncTask or in a Thread.

提交回复
热议问题