尝试运行Google Search Api时出现StringIndexOutofBoundsException

时间:2014-12-23 12:52:26

标签: java string

我正在尝试从以下SO链接运行谷歌搜索API: - How can you search Google Programmatically Java API 以下是我的代码: -

public class RetrieveArticles {

    public static void main(String[] args) throws UnsupportedEncodingException, IOException {
        // TODO Auto-generated method stub

        String google = "http://www.google.com/news?&start=1&q=";
        String search = "Police Violence in USA";
        String charset = "UTF-8";
        String userAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"; // Change this to your company's name and bot homepage!

        Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().children();

        for (Element link : links) {
            String title = link.text();
            String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
            url = URLDecoder.decode(url.substring(url.indexOf('=') +1, url.indexOf('&')), "UTF-8");

            if (!url.startsWith("http")) {
                continue; // Ads/news/etc.
            }
           System.out.println("Title: " + title);
           System.out.println("URL: " + url);
        }   
   }
}

当我尝试运行时,我收到以下错误。任何人都可以帮我修理它。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1911)
    at google.api.search.RetrieveArticles.main(RetrieveArticles.java:34)

提前致谢。

2 个答案:

答案 0 :(得分:2)

问题在于:

url.substring(url.indexOf('=') +1, url.indexOf('&'))

url.indexOf('=')url.indexOf('&')返回-1,这是subString中的非法参数。

在假设它包含url=之前,您应验证正在解析的&

答案 1 :(得分:0)

添加System.Out.Println(Url);

之前

url = URLDecoder.decode(url.substring(url.indexOf('=')+ 1,url.indexOf('&amp;')),“UTF-8”);

然后你就会知道,url字符串是否包含'=','&amp;'或不。