0

I'm attempting to retrieve the HTML code from a URL. In the past, I used an AsyncTask for this purpose, but it's now deprecated. I'm uncertain about the suitable alternative to use. My requirement involves not only fetching the HTML code but also searching within the HTML document for specific attributes to extract data.

Currently, I'm experimenting with a somewhat unconventional approach that doesn't seem to be effective. I'm wondering if anyone could provide guidance and assistance in resolving this issue.

here is the code im using

import java.util.concurrent.Future;
import java.util.concurrent.ExecutorService;


    List<String> ProfileHtmlList;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<List<String>> future = executor.submit(new FetchMetadataTask(linkUrl));
    try {
        ProfileHtmlList = future.get();
        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
        executor.shutdown();
        if (ProfileHtmlList != null) {
            for (String metadata : ProfileHtmlList) {
                Toast.makeText(getApplicationContext(), metadata, Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "Empty List", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    
    private class FetchMetadataTask implements Callable<List<String>> {
        private final String url;
    
        public FetchMetadataTask(String url) {
            this.url = url;
        }
    
        @Override
        public List<String> call() {
            List<String> metadataList = new ArrayList<>();
            try {
                Document document = (Document) Jsoup.connect(url).get();
                Elements metaTags = (Elements) document.getElementsByTagName("meta");
                for (Element metaTag : metaTags) {
                    String property = meta
Vasant Raval
  • 257
  • 1
  • 12
  • 31

0 Answers0