使用Google Feed api java

时间:2013-06-18 06:55:43

标签: java google-feed-api

我正在尝试使用Google Feed api获取一些数据。但是line = reader.readLine()始终为null。

URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
                    "v=1.0&q=Official%20Google%20Blog");
            URLConnection connection = url.openConnection();
            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());

1 个答案:

答案 0 :(得分:1)

试试这个

    URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
            "v=1.0&q=Official%20Google%20Blog");
    URLConnection connection = url.openConnection();

    ByteArrayOutputStream content = new ByteArrayOutputStream();

    InputStream is = connection.getInputStream();
    int len = 0;
    byte[] buffer = new byte[1024];
    while ((len = is.read(buffer)) >= 0)
    {
        content.write(buffer, 0, len);
    }
    byte[] finalContent = content.toByteArray();
    String str = new String(finalContent, "UTF8");
    System.out.print(str);

或者其他方式是你可以阅读content-length标题并阅读它,直到你获得那么多数据。