• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSOUP HTMl to ArrayList

I am really frustrated. I've been trying this for the past week but no success.
http://www.billboard.com/charts/artist-100 I am wanting to parse the names of the artists from here and I have done so with the code, but whenever I run the program, I get the ArrayList size is 0 and it is really frustrating at this point and I thought of just giving up.

Code:
package com.uw.aakarsh.myapplication2;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.util.Log;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.ListAdapter;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.io.*;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;

    public class MainActivity extends AppCompatActivity {
        ArrayList<String> Names = new ArrayList<String>();

        public class getName extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... Void) {
                Document doc;

                try {
                    String url = "http://www.billboard.com/charts/artist-100";
                    doc = Jsoup.connect(url).get();
                    Elements names = doc.select("div.chart-row__title > h2.chart-row__song");
                    for (Element p : names)
                        Names.add(p.text().toString());

                }
                catch(IOException ex){

                }

                return null;
            }
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            new getName().execute();
            System.out.println(Names.size());
        }
    }

This is my whole class. It always prints that the arraylist has no size. What do I do?
 
Names is 0 size at line 64 because this method is not synchronous

Code:
new getName().execute();

This starts a thread to run in the background, in parallel with your main thread.

So the code which calls the remote web service hasn't yet finished when the main thread calls System.out.println()

The bottom line is, you didn't wait long enough for the background task to finish.

This might be a good time for you to review how AsyncTask works:

https://developer.android.com/reference/android/os/AsyncTask.html

And more generally, multithreading:

https://developer.android.com/guide/components/processes-and-threads.html
 
Last edited by a moderator:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones