0

AI have some problems again))

I have a code which parse links from file. Into the file i have some links to forum treads. Like this:

http://vao-priut.org/image/cherepashka-metis-gollandskoi-ovcharki-s-72 http://vao-priut.org/image/taiga-s-26

and etc.

I try to parse images with this code:

class ParseMyPageTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            input = new URL("https://gist.githubusercontent.com/akhorevich/5b849373dc9abaf921b3/raw/18e79ab6a0c0be007a2a4590e4e176184ced311a/links");

            sc = new Scanner(input.openStream());
            while(sc.hasNextLine()){
                String link = sc.nextLine();

                doc = Jsoup.connect(link).get();
               Elements names = doc.select("div.node-title");
              // Elements images = doc.select("div.node div.content img");
                Elements imgs = doc.select("div.node div.content img");

                for (Element img : imgs) {

                    Element myImage = img;
                    String imgSrc = myImage.attr("src");
                    InputStream inp = new java.net.URL(imgSrc).openStream();
                    // Decode Bitmap
                    bitmap = BitmapFactory.decodeStream(inp);
                }


                for(Element name: names) {
                    mData.add(name.text());

                }

                if(mData.size() == 0) {
                    mData.add("Empty result");
                }

            }



        } catch (IOException e) {
            e.printStackTrace();
            mData.clear();
            mData.add("Exception: " + e.toString());
        }
        return text; // получаем весь текст
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        sectorC_adapter = new SectorC_Adapter(getActivity());
        mListView.setAdapter(sectorC_adapter);

    }
}

But it parse image only from last link and add it to all items. How can i make some images bufferedArray or something like this? Thank you!!!

P.S. I forget about my BaseAdapter:

class SectorC_Adapter extends BaseAdapter{
    private Context c;

    SectorC_Adapter(Context c){
        this.c = c;
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            inflater = (LayoutInflater)c
                    .getSystemService(c.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.sector_row, parent,false);
        }else {
            row = convertView;
        }
        tvInfo = (TextView)row.findViewById(R.id.dog_name);
        tvInfo.setText(mData.get(position).toString());
        dog_view = (ImageView)row.findViewById(R.id.dog_view);
        dog_view.setImageBitmap(bitmap);
        return row;
    }
}

1 Answer 1

1

In here you are not adding your image to any list

            for (Element img : imgs) {

                Element myImage = img;
                String imgSrc = myImage.attr("src");
                InputStream inp = new java.net.URL(imgSrc).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(inp);

               ///HERE YOU SHOULD ADD YOUR BITMAP TO ALL ELEMENTS
               ///eg. allImages.add(bitmap); or whatever.
            }

I suppose that bitmap is some sort of field that is added to your all elements so basicly only the last state will be stored. What you have to do is to add your bitmap inside for-loop.

6
  • Thank. I forgot to mention that i have a custom BaseAdapter which loads parsed info into ListView. Maybe i have some errors in it... Apr 1, 2015 at 9:10
  • Its work, but now i have another problem - it parse all photos from first link. Apr 1, 2015 at 19:00
  • You are storring only one image insteed of all of them. Apr 1, 2015 at 19:03
  • Can you explain me how to store all images what i need? Apr 6, 2015 at 8:30
  • Analyse targeted website and get repetitive elements that you need. Use firebug plugin for firefox. in my experie ce this is the best way to do it Apr 6, 2015 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.