Downloading images from Google Image search using Java

风流意气都作罢 提交于 2019-12-25 17:06:15

问题


I am trying to write a Java code that should fetch me the results of the google image search. Later on, I also want to download all the images from Google Image search given a query. Right now, I have written this code but when I run it, it is giving the error that "JSONObject["responseData"] is not a JSONObject."

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.fasterxml.jackson.core.JsonParser;

import twitter4j.JSONObject;

public class TestImage {

public static void main(String[] args) 
{
    try
    {
        URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                "v=1.0&q=barack%20obama&userip=INSERT-USER-IP");
        URLConnection connection = url.openConnection();
        connection.addRequestProperty("Referer", "");

        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((line = reader.readLine()) != null) 
        {
            builder.append(line);
        }

        JSONObject json = new JSONObject(builder.toString());
        String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");
       // JsonParser jsonParser = new JsonParser();
        //((Object) jsonParser).parse(json).getAsJsonObject();
        BufferedImage image = ImageIO.read(new URL(imageUrl));
        JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image));
    } 
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }
}

}`

Please help me with what is the error as I am very new to Java.


回答1:


Answering your question, it is quite simple. The response you are getting back from your

String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");

is not a valid JSONObject. Double check your source and make sure it is a json response.

Also note that the API you are using is quite outdated.




回答2:


This is because the API is deprecated so JSONObject["resultData"] is returning a null value.

To confirm this, simply enter your search URL (https://ajax.googleapis.com/ajax/services/search/images?" + "v=1.0&q=barack%20obama&userip=INSERT-USER-IP) into the top bar of your browser and google it. You will see that Google no longer provides the data for the images.

To my knowledge, there is not yet a very good replacement, although I've heard the Google Custom Search (https://developers.google.com/custom-search/json-api/v1/reference/cse/list) can work (although you only get 100 free queries per day and must pay after that).



来源:https://stackoverflow.com/questions/36002916/downloading-images-from-google-image-search-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!