1

Google フィード 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());
4

1 に答える 1

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ヘッダーを読み取って、それだけのデータを取得するまでそれを読み取ることができます。

于 2013-06-18T14:22:55.517 に答える