1

I'm doing a simple JSON grab from two links with the same code. I'm doing it two separate times, so the cause of my issue isn't because they're running into each other or something.

Here is my code:

@Override
        protected String doInBackground(Object... params) {
            try {
                URL weatherUrl = new URL("my url goes here");
                HttpURLConnection connection = (HttpURLConnection) weatherUrl
                        .openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);
                    int contentLength = connection.getContentLength();
                    char[] charArray = new char[contentLength];
                    reader.read(charArray);
                    String responseData = new String(charArray);
Log.v("test", responseData);

When I try this with:

http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json

I get an error of having an array lenth of -1

For this link:

http://api.openweathermap.org/data/2.5/weather?id=5815135

It returns fine and I get a log of all of the JSON. Does anyone have any idea why?

Note: I tried stepping through my code in debug mode, but I couldn't catch anything. I also downloaded a Google chrome extension for parsing json in the browser and both urls look completely valid. I'm out of ideas.

EGHDK
  • 17,818
  • 45
  • 129
  • 204

1 Answers1

3

Log this: int contentLength = connection.getContentLength();

I don't see the google url returning a content-length header.

If you just want String output from a url, you can use Scanner and URL like so:

Scanner s = new Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\\A");
out = s.next();
s.close();

(don't forget try/finally block and exception handling)

The longer way (which allows for progress reporting and such):

String convertStreamToString(InputStream is) throws UnsupportedEncodingException {

      BufferedReader reader = new BufferedReader(new    
                              InputStreamReader(is, "UTF-8"));
      StringBuilder sb = new StringBuilder();
      String line = null;
      try {
          while ((line = reader.readLine()) != null)
              sb.append(line + "\n");
      } catch (IOException e) {
          // Handle exception
      } finally {
          try {
              is.close();
          } catch (IOException e) {
              // Handle exception
          }
      }
      return sb.toString();
   }
}

and then call String response = convertStreamToString( inputStream );

323go
  • 14,143
  • 6
  • 33
  • 41
  • Do you mean the first URL? – darrengorman Jul 31 '13 at 23:48
  • Content length is 400+ for the weather json, and it's -1 for the google json... so what does that mean to me? How is it not picking up the content? – EGHDK Jul 31 '13 at 23:50
  • 1
    `content-length` is a header set by the responding server. It is optional, and google chooses not to send it for dynamically generated content (most servers don't). So you'll have to read the stream until you're out of bytes, rather than doing it in one go. – 323go Jul 31 '13 at 23:53
  • Could you elaborate on that? I'm not entirely sure what you mean. Sorry! – EGHDK Aug 01 '13 at 00:00