0

Trying to figure out what I need to do next.

I have opened the stream, buffered it, but I really don't know how to take that BufferedReader and send my method 'createFacebookCoverObject' a string to be analyzed. Thanks for the help..

        public void testCreateFavebookCoverObject() throws Exception {
    System.out.println("createFavebookCoverObject");
    String url = "https://graph.facebook.com/19292868552/";
    InputStream is = new URL(url).openStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    FacebookCover instance = new FacebookCover();
    FacebookCover result = instance.createFacebookCoverObject(url);//this is where I get stuck
}
3
  • What do you want to do with the input from the BufferedReader? Do you know what format this input is in? Commented Oct 5, 2014 at 3:38
  • I just wanted to pull the JSON from the site, with no HTML. Commented Oct 5, 2014 at 15:17
  • Most likely the response only contains JSON and not HTML. However, the response still must conform to the HTTP specification. This means that the raw response data has HTTP header information that you need to parse. Commented Oct 5, 2014 at 22:11

1 Answer 1

1

The BufferedReader has read methods (I think readLine) that will return the input stream as strings. You can call use this to get the entire response. That may not be what you want however. Why? Because the response will contain HTTP headers and a body. Presumably you are only interested in the body which is where you will likely find the JSON string.

I suggest using a an HTTP API that will parse the response for you. A couple options include:

  1. Apache HTTP client - note this is 3rd party.
  2. Java's HTTPURLConnection should do the trick. The following post has example code:

How can I get an http response body as a string in Java?

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

2 Comments

Ah ok the HTTPURLConnection is good, however, I am getting an error with IOUtils.toString(in, encoding); It says I am not allowed to pass arguments, but clearly I should be able too.
@TheCrownedPixel What is the exact error message? And which version of HTTPURLConnection are you using?

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.