有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的结果字符串在解析json数据后不打印到logcat?

我正试图解析来自wunderground的Json提要。com并在logcat中显示结果,但在到达日志时不会打印任何内容。五、声明。我知道电话正在接通,因为它出现在《神鬼世界》的分析页面上。我使用的是Android Studio,没有出现任何错误或崩溃。它就是印不出来。如果是因为结果字符串为null,那么为什么其中没有json数据?请帮忙。谢谢

这是我的AsyncTask类:

class MySubAsyncTask extends AsyncTask<Object, Object, Object> {

    @Override
    protected String doInBackground(Object[] params) {

        String result;

        Log.d("TEST", "parse is working");

        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader reader = null;


        try {
            URL url = new URL("http://api.wunderground.com/api/MYAPIKEYHERE/forecast/geolookup/astronomy/q/FL/Tampa.json");
            connection = (HttpURLConnection) url.openConnection();
            inputStream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuilder theStringBuilder = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                theStringBuilder.append(line);
                theStringBuilder.append("\n");
            }

            result = theStringBuilder.toString();

            Log.v("TEST","Full JSON string = ");
            Log.v("TEST", result);

            return result;


        } catch (java.io.IOException e) {
            Log.d("TEST","IO Error 1");
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.d("TEST","IO Error 2");
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    Log.d("TEST","IO Error 3");
                    e.printStackTrace();
                }
            }
        }
        Log.d("TEST", "Log after finally");

        return null;
    }

}

我在logcat中得到的唯一输出是前两条日志语句:

04-19 22:23:03.022411-2948/com。伊莱。myweatherapp D/TEST:解析正在工作

04-19 22:23:03.8252411-2948/com。伊莱。myweatherapp V/TEST:完整JSON字符串=

带有我的结果字符串的log语句和finally块后的log statement都不打印,我也不知道为什么

编辑: 我在连接之前添加了此代码。getInuptStream()

                StringBuilder builder = new StringBuilder();
            builder.append(connection.getResponseCode())
                    .append(" ")
                    .append(connection.getResponseMessage())
                    .append("\n");

            Map<String, List<String>> map = connection.getHeaderFields();
            for (Map.Entry<String, List<String>> entry : map.entrySet())
            {
                if (entry.getKey() == null)
                    continue;
                builder.append( entry.getKey())
                        .append(": ");

                List<String> headerValues = entry.getValue();
                Iterator<String> it = headerValues.iterator();
                if (it.hasNext()) {
                    builder.append(it.next());

                    while (it.hasNext()) {
                        builder.append(", ")
                                .append(it.next());
                    }
                }

                builder.append("\n");
            }

            Log.d("TEST",builder.toString());

现在它将此打印到logcat:

04-20 20:19:10.364 14680-15085/com。伊莱。myweatherapp D/测试:200正常

访问控制允许凭据:true 访问控制允许来源:* 缓存控制:最大年龄=0,无缓存 连接:保持活力 内容类型:application/json;字符集=UTF-8 日期:2016年4月21日星期四00:19:10 GMT 到期时间:2016年4月21日星期四00:19:10 GMT 最后修改:2016年4月21日星期四00:19:10 GMT Pragma:没有缓存 服务器:Apache/2.2.15(CentOS) 设置Cookie:DT=1461197950:16390:365-u3;路径=/;expires=2020年1月1日星期五00:00:00 GMT;域=。无名小卒。com,Prefs=FAVS:1 | WXSN:1 | PWSOBS:1 | WPHO:1 | PHOT:1 | RADC:0 | RADALL:0 | HIST0:NULL | GIFT:1 | PHOTOTHUMBS:50 | EXPFCT:1 |;路径=/;expires=2020年1月1日星期五00:00:00 GMT;域=。无名小卒。通用域名格式 改变:接受编码 X-Android-Received-Millis:1461197950363 X-Android-Response-Source:网络200 X-Android-Sent-Millis:1461197949971 X-CreationTime:0.102


共 (1) 个答案

  1. # 1 楼答案

    如果值为空,记录器将不会打印该行

    所以Log.v("TEST", result);没有打印任何内容的具体原因是result是一个空字符串

    您可以添加日志语句来显示大小,以查看这一点

    我只是通过运行来测试这一点:

    Log.v("TEST","111");
    Log.v("TEST","");
    Log.v("TEST","333");
    

    finally之后可能没有任何日志,因为您已经在try块内返回了

    我还可以看到您的代码无法编译:在getInputStream之后没有分号

    connection = (HttpURLConnection) url.openConnection();
    inputStream = connection.getInputStream()
    reader = new BufferedReader(new InputStreamReader(inputStream));
    

    你确定这是你应用程序中的实际代码吗