How to get data from 2 or more websites at the same time?(Android app)

beg_dev :

I've made an app which gets data from different urls. I used a for loop to get data from different urls using jsoup lib. Now this takes a lot of time like 5 sec. First get data from url1 and then 2 and then 3.. This is what taking the time I think.

So I want to know whether we can get data from different urls at the same time(Multiple threads) or not?

public  class Title extends AsyncTask <String, Void, Void> {

    String url,ver;
    Bitmap mIcon11 = null;
    ArrayList<App> appsList = new ArrayList<>();

    @Override
    protected Void doInBackground(String ... strings) {
        try {

            for (String string : strings) {
                Document document = Jsoup.connect(string).get();

                Elements a = document.select("div.AppCont");
                Elements b = a.select("article");
                Elements c = b.select("div.ImgDiv");
                Elements d = c.select("img");
                url = d.attr("src");

                InputStream in = new URL(url).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);

                ver = b.get(0).text();
                String z = string.replace("https://a2zapk.com/History/", "");
                z = z.replace("/", "");

                PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo((z), PackageManager.GET_META_DATA);
                String versionName = pi.versionName;

                ver = ver + " (Installed Version: " +versionName + ")";
                appsList.add(new App(ver, mIcon11));

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
         add(appsList);
    }
Dennis :

You absolutely can, but managing the threading for these tasks can get tricky. I would recommend using Rxava to prepare a separate observable for each site you'd like to fetch data from. Then use either merge or mergeDelayError to merge results into a single Observable that you can subscribe to on the main thread to update your UI.

Check out RxJavaAndroid for help subscribing to these updates on Android's main thread.

You'll want to be familiar with 4 core pieces of RxJava:

  1. What you're doing - In your case, this is fetching data from a server
  2. What thread pool is running this task - I'd recommend Schedulers.io() which is a pool set aside specifically for IO tasks like fetching data.
  3. What thread pool you'll be observing results on - AndroidSchedulers.mainThread() is what you'll want here
  4. What to do with the results - Update some UI, etc.

This would look something like the following using RxJava (in Kotlin)

// What you're doing
Observable.fromCallable {
    listOfApps = parseAppsList(Jsoup.connect("server1.host.com"))
    return@fromCallable listOfApps
}
        // Where you're doing it
        .subscribeOn(Schedulers.io())
        // Where you're observing results
        .observeOn(AndroidSchedulers.mainThread())
        // What you're doing with those results
        .subscribe({ apps ->
            appsList.addAll(apps)
        }, { exception -> 
            // Show an error message
        })

To fetch multiple results simultaneously and add them as each finishes, your code would look something like this:

val fromServer1 = Observable.fromCallable {
    listOfApps = parseAppsList(Jsoup.connect("server1.host.com"))
    return@fromCallable listOfApps
}
        .subscribeOn(Schedulers.io())


val fromServer2 = Observable.fromCallable {
    listOfApps = parseAppsList(Jsoup.connect("server2.host.com"))
    return@fromCallable listOfApps
}
        .subscribeOn(Schedulers.io())


Observable.merge(fromServer1, fromServer2)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ apps ->
            // Note that this callback will be called once per server
            appsList.addAll(apps)
        }, { exception ->
            // Show an error message
        })

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100531&siteId=1