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
}
TheCrownedPixel
  • 359
  • 6
  • 18

1 Answers1

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?

Community
  • 1
  • 1
EJK
  • 12,332
  • 3
  • 38
  • 55
  • 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 Oct 05 '14 at 19:19
  • @TheCrownedPixel What is the **exact** error message? And which version of HTTPURLConnection are you using? – Code-Apprentice Oct 05 '14 at 22:14