0

I have a list of number of url and its name is listOfUrlSources, I passed it inside class GenerateUrlImages the task of this class is to come out with a picture link and it works if you use it and pass a link to one site it gives out a picture link, but now I'm trying to pass more than one site for it and also give me links to photos I tried, but I didn't make it through a list.

// 
for (int i = 0; i < sources.size(); i++) {

                        item = new GenerateImage(sources.get(i).getUrl());
                        // networkImagesLiveData.setValue(createLiveDataForLoadingImage(sources.get(i).getUrl()));
                        listOfUrlSources.add(item);
                    }

                    new GenerateUrlImages().execute(listOfUrlSources);

//---------


// 
   public class GenerateUrlImages extends AsyncTask<List<GenerateImage>, Void, List<GenerateImage>>{

        public GenerateUrlImages(){

        }

        @Override
        protected List<GenerateImage> doInBackground(List<GenerateImage>... arrayLists) {
            List<String> result = new ArrayList<String>();
            List<GenerateImage> passed = new ArrayList<GenerateImage>();
            passed = arrayLists[0];

            //get passed arraylist
            //urlImages
            String src="";
            try {
                org.jsoup.nodes.Document doc1;
                Elements img2;
                GenerateImage Item;
                for(int i=0; i<passed.size(); i++) {
                  doc1 = Jsoup.connect(passed.get(i).getUrl()).get();
                  //Elements img = doc1.getElementsByTag("img");
                    img2 = doc1.getElementsByTag("meta");
                    for(Element element : img2) {
                      if("og:image".equals(element.attr("property"))) {
                        src = element.attr("content");
                         Item = new GenerateImage(src);
                          urlImages.add(Item);
                       }
                    }
                    for (int j = 0; j < urlImages.size(); j++) {
                        Log.d("getUrlImage:", "" + urlImages(j));
                    }
                }
                //InputStream in = new java.net.URL(src).openStream();
               // bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error :", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPreExecute(){

        }
        protected void onPostExecute(ArrayList<String> result){


        }

    }
ibrahem ahmed
  • 25
  • 1
  • 4
  • Error is in this line `Log.d("getUrlImage: ", "" + urlImages);` if you add a space the exception will go away like `Log.d("getUrlImage: ", " " + urlImages);` and for checking why the list is empty, use add checkpoints – hfarhanahmed Feb 04 '20 at 11:53

2 Answers2

0

You have printed the Arraylist without index value using the ArraylistName.get(index) in your case Log.d("getUrlImage: ", "" + urlImages); it should be like Log.d("getUrlImage: ", "" + urlImages.get(j));

after this change in your code, you will be able to see the required result.

for (int j = 0; j < urlImages.size(); j++) {
     Log.d("getUrlImage:  ", "" + urlImages.get(j));
      }
Vishal
  • 184
  • 7
0

In for loop you did a mistake

for (int j = 0; j < urlImages.size(); j++) {
      Log.d("getUrlImage:  ", "" + urlImages[j]); // add this line also
}
Arup
  • 54
  • 1
  • 8