Reputation: 23
I'm trying to parse a Json feed from wunderground.com and display the result in the logcat but nothing prints when it gets to the Log.v statement. I know call is going through because it shows up on the analitics page on wunderground. I'm using Android Studio and I'm not getting any errors or crashes. It just doesn't print. And if its because the result string is null why does it not have the json data in it? Please help. Thanks.
this is my AsyncTask class:
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;
}
}
The only output i get in the logcat is the first two Log statements:
04-19 22:23:03.022 2411-2948/com.eli.myweatherapp D/TEST: parse is working
04-19 22:23:03.825 2411-2948/com.eli.myweatherapp V/TEST: Full JSON string =
The log statement with my result string and the log staement after the finally block both don't print and i have no idea why.
edit: i added this code before 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());
now it prints this to the 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 Date: Thu, 21 Apr 2016 00:19:10 GMT Expires: Thu, 21 Apr 2016 00:19:10 GMT Last-Modified: Thu, 21 Apr 2016 00:19:10 GMT Pragma: no-cache Server: Apache/2.2.15 (CentOS) Set-Cookie: DT=1461197950:16390:365-u3; path=/; expires=Fri, 01-Jan-2020 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|; path=/; expires=Fri, 01-Jan-2020 00:00:00 GMT; domain=.wunderground.com Vary: Accept-Encoding X-Android-Received-Millis: 1461197950363 X-Android-Response-Source: NETWORK 200 X-Android-Sent-Millis: 1461197949971 X-CreationTime: 0.102
Upvotes: 1
Views: 829
Reputation: 664
The logger will not print the line if the value is empty.
So the specific reason that Log.v("TEST", result);
is not printing anything, is because result
is an empty string.
You can add log statements to show the size to see this as well.
I tested this just by running:
Log.v("TEST","111");
Log.v("TEST","");
Log.v("TEST","333");
You are probably not getting any logs after the finally
, because you are already returning inside the try
block.
I can also see that your code would not compile: you have no semicolon after getInputStream
.
connection = (HttpURLConnection) url.openConnection();
inputStream = connection.getInputStream()
reader = new BufferedReader(new InputStreamReader(inputStream));
Are you sure this is the actual code you have in your app?
Upvotes: 0