[英]Why doesn't my result string print to the logcat after parsing json data?
我正在尝试从wunderground.com解析一个Json提要并在logcat中显示结果,但是当它到达Log.v语句时没有打印。 我知道电话正在通过,因为它出现在wunderground的analitics页面上。 我正在使用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中获得的唯一输出是前两个Log语句:
04-19 22:23:03.022 2411-2948 / com.eli.myweatherapp D / TEST:解析工作正常
04-19 22:23:03.825 2411-2948 / com.eli.myweatherapp V / TEST:完整的JSON字符串=
带有我的结果字符串的日志语句和finally块之后的日志文件都不打印,我不明白为什么。
编辑:我在connection.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.eli.myweatherapp D / TEST:200 OK
Access-Control-Allow-Credentials:true Access-Control-Allow-Origin:* Cache-Control:max-age = 0,no-cache Connection:keep-alive Content-Type:application / json; charset = UTF-8日期:星期四,2016年4月21日00:19:10 GMT到期:星期四,2016年4月21日00:19:10 GMT Last-Modified:星期四,21四月2016 00:19:10 GMT Pragma:no-缓存服务器:Apache / 2.2.15(CentOS)Set-Cookie:DT = 1461197950:16390:365-u3; 路径= /; expires =周五,01年1月1日至20:00 00:00 GMT; domain = .wunderground.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 =周五,01年1月1日至20:00 00:00 GMT; domain = .wunderground.com变化:接受编码X-Android-Received-Millis:1461197950363 X-Android-Response-Source:NETWORK 200 X-Android-Sent-Millis:1461197949971 X-CreationTime:0.102
如果值为空,记录器将不会打印该行。
那么Log.v("TEST", result);
的具体原因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));
您确定这是您应用中的实际代码吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.