Google搜索API返回false

时间:2014-07-23 00:45:54

标签: java google-app-engine search

我的工具代码使用Google搜索API。

我的代码:

import com.google.gson.Gson;

class GoogleResults {

    private ResponseData responseData;

    public ResponseData getResponseData() {
        return responseData;
    }

    public void setResponseData(ResponseData responseData) {
        this.responseData = responseData;
    }

    public String toString() {
        return "ResponseData[" + responseData + "]";
    }

    static class ResponseData {
        private List<Result> results;

        public List<Result> getResults() {
            return results;
        }

        public void setResults(List<Result> results) {
            this.results = results;
        }

        public String toString() {
            return "Results[" + results + "]";
        }
    }

    static class Result {
        private String url;

        public String getUrl() {
            return url;
        }


        public void setUrl(String url) {
            this.url = url;
        }


        public String toString() {
            return "Result[url:" + url + " ]";
        }
    }
}

public class CrawData {

    public static void main(String[] args) throws IOException, InterruptedException {

        String query;
        int n;
        int k=0;
        String site;
        String resultset;
        Scanner st = new Scanner(System.in);
        System.out.print(" Input key search: ");
        query = st.nextLine();

        System.out.print("Input site: ");
        site = st.nextLine();

        System.out.print("Input number of result: ");
        n = st.nextInt();

        resultset = query + " site:" + site;


        for (int j = 0; j < n; j = j + 1) {

            Thread.sleep(4000);
            String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+j+"&q=";
            String charset = "UTF-8";



            URL url = new URL(address + URLEncoder.encode(resultset, charset));
            Reader reader = new InputStreamReader(url.openStream(), charset);
            GoogleResults results = new Gson().fromJson(reader,
                    GoogleResults.class);

            int total = results.getResponseData().getResults().size();

            // Show title and URL of each results
            for (int i = 0; i <= total - 1; i++) {

                String Url = results.getResponseData().getResults().get(i)
                        .getUrl();
                k = k+1;
                System.out.println("URL: " +Url+ " " + k);

            }
        }
    }
}

当我运行它时,我遇到了代码返回结果的问题。

我的网站系统返回列表网址..但不稳定。

一些图片:

my error

有错误:线程中出现异常&#34; main&#34;显示java.lang.NullPointerException     在CrawData.main(CrawData.java:107)

帮帮我......

抱歉,我的英语太差了...... :(

1 个答案:

答案 0 :(得分:0)

我的猜测是在这一行:

String Url = results.getResponseData().getResults().get(i)
                        .getUrl();

get(i)返回null或getUrl()返回null。您应该添加一些错误处理逻辑:

if (results.getResponseData().getResults().get(i) != null && 
results.getResponseData().getResults().get(i).getUrl() !=null) {
  String Url = results.getResponseData().getResults().get(i)
                       .getUrl();
  k = k+1;
  System.out.println("URL: " +Url+ " " + k);
} else {
  // Print some type of error here.  Try to figure out why the result or the 
  // url is null
}