简体   繁体   中英

How to get the content of a Website to a String in Android Studio ?

I want to display the parts of the content of a Website in my app. I've seen some solutions here but they are all very old and do not work with the newer versions of Android Studio. So maybe someone can help out.

https://jsoup.org/ should help for getting full site data, parse it based on class, id and etc. For instance, below code gets and prints site's title:

Document doc = Jsoup.connect("http://www.moodmusic.today/").get();
String title = doc.select("title").text();
System.out.println(title);

If you want to get raw data from a target website, you will need to do the following:

  • Create a URL object with the link of the website specified in the parameter
  • Cast it to HttpURLConnection
  • Retrieve its InputStream
  • Convert it to a String

This can work generally with java, no matter which IDE you're using.

To retrieve a connection's InputStream:

// Create a URL object
URL url = new URL("https://yourwebsitehere.domain");
// Retrieve its input stream
HttpURLConnection connection = ((HttpURLConnection) url.openConnection());
InputStream instream = connection.getInputStream();

Make sure to handle java.net.MalformedURLException and java.io.IOException

To convert an InputStream to a String

public static String toString(InputStream in) throws IOException {
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;
    while ((line = reader.readLine()) != null) {
        builder.append(line).append("\n");
    }
    reader.close();
    return builder.toString();
}

You can copy and modify the code above and use it in your source code!

Make sure to have the following imports

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

Example:

public static String getDataRaw() throws IOException, MalformedURLException {
    URL url = new URL("https://yourwebsitehere.domain");
    HttpURLConnection connection = ((HttpURLConnection) url.openConnection());
    InputStream instream = connection.getInputStream();
    return toString(instream);
}

To call getDataRaw(), handle IOException and MalformedURLException and you're good to go!

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM