1

I'm trying to fetch some data using Google feed api. But line = reader.readLine() is always 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
  • How you know its null, try to print line inside your while loop and check what actually is coming Commented Jun 18, 2013 at 7:17
  • I checked with a debugger. Commented Jun 18, 2013 at 7:33
  • I tried your code and i am able to get the data without any modification. I recommend you put a print statement in your while loop and check. Commented Jun 18, 2013 at 8:39
  • You were right. But there is another problem.The code's is getting not entire data Commented Jun 18, 2013 at 13:30

1 Answer 1

1

Try this

    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);

Or other way is you can read the content-length header and read that till you get that much data.

Sign up to request clarification or add additional context in comments.

1 Comment

I found out that logcat didn't print entire data. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.