JSON拼抢从工作一个环节,而不是从另一个 [英] Grabbing JSON works from one link, not from another

查看:85
本文介绍了JSON拼抢从工作一个环节,而不是从另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做从具有相同code两个环节一个简单的JSON抢。我做的两个单独的时间,所以我的问题的原因并不是因为他们正在运行到对方或东西。

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.

下面是我的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);

当我尝试这跟:

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

我得到具有-1阵列宽度对错误

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

有关此链接:

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

它返回正常,我得到一个日志中的所有JSON的。没有人有任何想法,为什么?

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

请注意:我试图通过在调试模式下我的code步进,但我没能赶上任何东西。我也下载了谷歌Chrome扩展程序在浏览器解析JSON和两个URL看起来完全有效。我的想法。

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.

推荐答案

日志这样的: INT CONTENTLENGTH = connection.getContentLength();

我不明白谷歌的网址返回内容长度头。

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

如果你只是想从一个URL字符串输出,可以使用扫描网​​址像这样:

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

(不要忘记尝试/ finally块和异常处理)

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

,然后调用字符串的响应= convertStreamToString(InputStream的);

and then call String response = convertStreamToString( inputStream );

这篇关于JSON拼抢从工作一个环节,而不是从另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆