1

First sorry for my bad english. Im new in programming (2 year in collage). Im trying to get image from zomato. Yep, API only give us link. ( https://www.zomato.com/jakarta/ayam-keprabon-express-tambora/photos) so I need to manually get the image from web.

I already done this in iOS using Kana.

func parseHTML(html:String){
    if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8){
        for show in doc.css("div[class^='photobox']"){
            let getA = show.css("a").first
            let image = getA!.css("img[class^='res-photo']").first?["data-original"]
            let imageUrl = image!.components(separatedBy: "?")[0]
            linkImageArray.append(imageUrl)
            print("jumlah gambar = \(linkImageArray.count)")
        }
    }
    foodCV.reloadData()
}

And my question how to do that in Jsoup?

This how i got HTML

Call<ResponseBody> photos = client.getPhotos();
    photos.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()){
                    Log.i("retrofit: ","response "+response.body().toString());
                    getPhotos(response.body().toString());
            }else{
                Log.i("retrofit: ","notSuccess");
            }
        }

Now im stuck with this. Photobox.toString empty.

public void getPhotos(String stringResponse){
    Document doc = Jsoup.parse(stringResponse);
    Elements photobox = doc.getElementsByClass("container");
    Log.i("html : ",""+photobox.toString());
    Toast.makeText(this, photobox.toString(), Toast.LENGTH_SHORT).show();
    for (Element data:photobox){
        Log.i("html : ",""+data.select("img[class^='res-photo']"));
        Log.i("html : ",""+data.select("img[class^='res-photo']").first().select("data-original"));
        data.select("img[class^='res-photo']").first().select("data-original");
    }
}

And im sure i cannot use this Jsoup: how to get an image's absolute url?. It will return all image. I would like to get img with 'data-original'.

<img class='res-photo-thumbnail thumb-load lazy-photo-inner'
        src='https://b.zmtcdn.com/images/photoback.png'
        title='Coffee Sisters - Coffee Sisters&#039;s photo'
        alt='Coffee Sisters - Coffee Sisters&#039;s photo'
        data-original='https://b.zmtcdn.com/data/reviews_photos/e4a/04b8e0669945bfe03fd0789cbd42be4a.jpg?fit=around%7C200%3A200&amp;crop=200%3A200%3B%2A%2C%2A'
        data-type='res'
        data-photo_id='u_NDY5NzEwNzI1MD'
        data-index='27'
        data-category='all'
         />
  • Possible duplicate of [Jsoup: how to get an image's absolute url?](https://stackoverflow.com/questions/4875064/jsoup-how-to-get-an-images-absolute-url) – K.Nicholas Jul 24 '17 at 16:27
  • @KarlNicholas It will return all img tag, i only need to get img with 'data-original'. – Tanurwijaya Jul 24 '17 at 16:56

1 Answers1

0

Just changing some lines of code i get what i want~

Call<ResponseBody> photos = client.getPhotos();
    photos.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()){
                    Log.i("retrofit: ","response "+response.body().toString());
                    getPhotos(response.body());


            }else{
                Log.i("retrofit: ","notSuccess");
            }
        }

I only change to pass response.body, instead response.body.string.toString .

public void getPhotos(ResponseBody body){
    try {
        Document doc = Jsoup.parse(body.string());
        Elements photobox = doc.select("img[class=res-photo-thumbnail thumb-load lazy-photo-inner]");Toast.LENGTH_SHORT).show();
        for (Element data : photobox) {
            String imagelink = data.attr("data-original").toString();
            String[] parts = imagelink.split("[?]");
            imageList.add(parts[0]);
            Log.i("imagelink : ",parts[0]);
        }
    }catch (IOException e){
        e.printStackTrace();
    }
}