【问题标题】:Android strange bug while downloading a JSON下载 JSON 时的 Android 奇怪错误
【发布时间】:2015-07-27 22:22:36
【问题描述】:

我需要下载一个字符串中的 JSON,我几乎可以确定代码没有错,但是有些奇怪...

public boolean downloadJSON(){
    json="";
    try{
        URL url = new URL(theURL);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        while ((line = in.readLine()) != null) {
            json+=line;
        }
        in.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

基本上它会正确打开缓冲区,正确读取 json 并将其存储在变量中,然后关闭流,然后返回 true 并且......它随机跳转到 return false 没有意义,而它在返回 false 异常 e 是... null O.o 这完全没有意义,请帮助我D:!!!

最后我是这样解决的:

public boolean downloadJSON(){
    json="";
    boolean out=false;
    try{
        URL url = new URL(theURL);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        while ((line = in.readLine()) != null) {
            json+=line;
        }
        in.close();
        out = true;
    } catch (Exception e) {
    }
  return out;
}

如果第一个不起作用,不要问我这应该如何正确,在我看来,我仍然认为第一个应该起作用:S

添加:为斯蒂芬 C:

public boolean downloadJSON(){
        json="";
        try{
            URL url = new URL("http://fapfapfap.altervista.org/conapo/conapo.php?n="+numeroPagina);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            while ((line = in.readLine()) != null){json+=line;}
            in.close();
            return true;
        }catch (Exception e) {
            Log.d("ERRORE", e.getStackTrace().toString());
            return false;
        }
    }

【问题讨论】:

  • 什么是 null O.o ?是 NullPointerException 吗?
  • 不可能。如果返回 false,e 绝对不为空。在 catch 块中返回之前添加它。 e.printStackTrace()
  • 可以提供网址吗?如果是离线 URL,是否只是返回 JSON?
  • url为:fapfapfap.altervista.org/conapo/conapo.php?n=1,被代码正确读取并保存在json字符串中
  • 好吧,我解决了,但还是谢谢 :D

标签: java android json download


【解决方案1】:

试试这个:

URL url = new URL(theURL);
StringBuilder jsonResults = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }

System.out.println(jsonResults.toString());

【讨论】:

  • 谢谢,这项工作,无论如何,我的代码毫无例外地跳转到 false 仍然没有任何意义:S
  • 你正在使用字符串来追加数据,而不是你应该像这样使用 stringbuilder .. StringBuilder stringbuilder = new StringBuilder(); BufferedReader bfrd = new BufferedReader(new InputStreamReader(in), 1024);字符串线; while ((line = bfrd.readLine()) != null) stringbuilder.append(line);下载的String = stringbuilder.toString();
  • 如果我的回答对你有帮助,请标记为正确,谢谢
【解决方案2】:

....我的代码毫无例外地跳转到 false 仍然没有任何意义

你误解了证据:

public boolean downloadJSON(){
    json = "";
    try {
        URL url = new URL(theURL);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(url.openStream()));
        String line;
        while ((line = in.readLine()) != null) {
            json += line;
        }
        in.close();
        return true;
    } catch (Exception e) {
        return false;  // HERE
    }
}

当您看到我标记为// HERE 的声明时。它发生了>>因为

(尝试在return false;前添加e.printStacktace();

我怀疑它要么是MalformedURLException,要么是FileNotFoundException……但也可能是其他东西。


那么你在这段代码中的错误是什么?

1) 捕捉Exception 是个坏主意。问题是它捕获的太多了。除了捕获您(可能)此时预期的异常之外,它还将捕获所有 unexpected 异常。例如,任何 NPE 或 AIOOPE 或任何由代码中的(假设)错误引起的。

在这种情况下,您可能应该捕获此时可能出现的异常;即IOException

2) 捕获异常并丢弃堆栈跟踪使诊断问题变得困难。如果您知道(并且可以证明)异常将是您所期望的,则建议“压制”这样的异常。

3) 在同一段代码中同时执行 1) 和 2) 是一个非常糟糕的主意。

4) 构建这样的字符串是低效的(或者非常低效......如果你正在加载一个非常大的文档)。使用StringBuilder 会好很多。

但请注意,4) 与您遇到的问题无关。

【讨论】:

  • 我一步一步调试代码,json读取成功,一切正常,甚至继续返回true,但是当我按F8返回true时,它在返回时跳转假的,就像如果 return true 是问题...而且无意义的是,当它在 return false 时,异常为空,就像它从未进入 catch :S 无论如何现在我通过做我现在写的来修复它在我的回答中编辑
  • 我知道我在说什么完全没有意义,如果我要回答这个答案,我也可能会说有某种例外,但没有 D:
  • @FedericoCapece - 您的“解决方案”与原始版本一样糟糕。你仍在捕捉和挤压Exception。不好。
  • 要么你是>>自欺欺人e.printStackTrace() 并且会引发 NPE,我只会相信您。
  • 我现在就说吧,我现在添加了我当前的代码并且......在logcat中什么都没有,并且代码仍然从return true跳转到return false,我知道你不能相信我,没有意义,如果你愿意我可以制作调试视频
猜你喜欢
  • 1970-01-01
  • 2013-11-07
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2017-06-01
相关资源
最近更新 更多