简体   繁体   中英

Getting the url of an image with jsoup

I am using google image search to look for images and get the URL of the first image in my android application using JSOUP library My problem is that no matter how much I try It shows me that the URL is null looks like the element that contains the first image does not have a url

here is the XML file:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="arb.myapplication.MainActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="131dp"
    android:id="@+id/imageView" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="39dp"
    android:layout_marginEnd="39dp"
    android:layout_marginTop="11dp"
    android:id="@+id/button3"
    android:elevation="0dp" />

Ignore the image view I use the button to get the url of the image here is the JAVA file

package arb.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;


public class MainActivity extends AppCompatActivity {

class DownloadIcon extends AsyncTask<Object, Object, Integer>
{

    @Override
    protected Integer doInBackground(Object... objects) {
        Connection connecion= Jsoup.connect("http://www.google.com/search?tbm=isch&q=facebook+logo");
        try {
            Document document=connecion.get();
            Elements element=document.select("img.rg_ic.rg_i");

            String url=element.first().absUrl("src");
            publishProgress(url);

        } catch (IOException e1) {
            publishProgress(-1);
        }
        return 0;
    }

    @Override
    protected void onProgressUpdate(Object... values) {
       /* ImageView imageView=(ImageView) findViewById(R.id.imageView);
        try {
            InputStream inputStream= new URL(values[0].toString()).openStream();
            imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        Toast.makeText(getBaseContext(),values[0].toString(),Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostExecute(Integer integer) {
        Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button3=(Button) findViewById(R.id.button3);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DownloadIcon().execute();

        }
    });
}

}

in the onProgressUpdate function does not show the Toast because its text is null

I already tried to get the attribute keys and values of the chosen element It matches those displayed when I parse the element using the Chrome explorer but without src attribute

The text is null because you get no images. If you debug your request you'll see 403 status code which tells you that Jsoup client is not authorized to fetch images from Google. You need to use or emulate browser to achieve your goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM