Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App Concurrency and Error Handling Making Our Code Asynchronous

Armand Ramos
Armand Ramos
6,002 Points

Darksky Response is giving me "{"time":1531270080,"precipIntensity":0,"precipProbability":0}" over and over again.

I'm following along the weather app video series for android dev and I'm at the point where we finally got the app to work properly for the first time. We're logging the results to the logcat and I'm getting the expected results but then it extends to repeating {"time":1531270080,"precipIntensity":0,"precipProbability":0}{"time":1531270080,"precipIntensity":0,"precipProbability":0}{"time":1531270080,"precipIntensity":0,"precipProbability":0}{"time":1531270080,"precipIntensity":0,"precipProbability":0}{"time":1531270080,"precipIntensity":0,"precipProbability":0}{"time":1531270080,"precipIntensity":0,"precipProbability":0} over and over again.

Armand Ramos
Armand Ramos
6,002 Points

I should note that the time is counting up so I feel like there's a loop being executed somehow but only on those specific response properties somehow? Not sure.

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.os.Bundle;
import android.util.Log;
import android.view.textclassifier.TextLinks;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String apiKey = "** HIDDEN **";

        double latitude = 37.8267;
        double longitude = -122.4233;

        String forecastURL ="https://api.darksky.net/forecast/"
                +apiKey+"/"+latitude+","+longitude;

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(forecastURL)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {

                    if(response.isSuccessful()){
                        Log.v(TAG, response.body().string());
                    }

                }   catch(IOException e) {
                        Log.e(TAG, "IT Exception caught:", e);
                }
            }
        });

    }
}

[MOD: edited code block & hid API key - srh]

If you post the code where you pull this JSON, that might help identify any problems with it.

Steve.

1 Answer

Hi there,

This output is correct. The JSON produces three sections of weather data - minutely, hourly and daily - see here. Within the minutely JSON data, you just get the timestamp and the precipitation data. For hourly and daily, there is more data brought back.

Your code is working fine.

Steve.

Armand Ramos
Armand Ramos
6,002 Points

kk, so just to clarify my log should be '{"latitude":37.8267,"longitude":-122.4233,"timezone":"America/Los_Angeles","currently":{"time":1531327349,"summary":"Mostly Cloudy","icon":"partly-cloudy-day","nearestStormDistance":25,"nearestStormBearing":70,"precipIntensity":0,"precipProbability":0,"temperature":59.46,"apparentTemperature":59.46,"dewPoint":54.24,"humidity":0.83,"pressure":1013,"windSpeed":4.52,"windGust":7.95,"windBearing":221,"cloudCover":0.66,"uvIndex":2,"visibility":10,"ozone":310.96},"minutely":{"summary":"Mostly cloudy for the hour.","icon":"partly-cloudy-day","data":[{"time":1531327320,"precipIntensity":0,"precipProbability":0},{"time":1531327380,"precipIntensity":0,"precipProbability":0},{"time":1531327440,"precipIntensity":0,"precipProbability":0},{"time":1531327500,"precipIntensity":0,"precipProbability":0},{"time":1531327560,"precipIntensity":0,"precipProbability":0},{"time":1531327620,"precipIntensity":0,"precipProbability":0},{"time":1531327680,"precipIntensity":0,"precipProbability":0},{"time":1531327740,"precipIntensity":0,"precipProbability":0},{"time":1531327800,"precipIntensity":0,"precipProbability":0},{"time":1531327860,"precipIntensity":0,"precipProbability":0},{"time":1531327920,"precipIntensity":0,"precipProbability":0},{"time":1531327980,"precipIntensity":0,"precipProbability":0},{"time":1531328040,"precipIntensity":0,"precipProbability":0},{"time":1531328100,"precipIntensity":0,"precipProbability":0},{"time":1531328160,"precipIntensity":0,"precipProbability":0},{"time":1531328220,"precipIntensity":0,"precipProbability":0},{"time":1531328280,"precipIntensity":0,"precipProbability":0},{"time":1531328340,"precipIntensity":0,"precipProbability":0},{"time":1531328400,"precipIntensity":0,"precipProbability":0},{"time":1531328460,"precipIntensity":0,"precipProbability":0},{"time":1531328520,"precipIntensity":0,"precipProbability":0},{"time":1531328580,"precipIntensity":0,"precipProbability":0},{"time":1531328640,"precipIntensity":0,"precipProbability":0},{"time":1531328700,"precipIntensity":0,"precipProbability":0},{"time":1531328760,"precipIntensity":0,"precipProbability":0},{"time":1531328820,"precipIntensity":0,"precipProbability":0},{"time":1531328880,"precipIntensity":0,"precipProbability":0},{"time":1531328940,"precipIntensity":0,"precipProbability":0},{"time":1531329000,"precipIntensity":0,"precipProbability":0},{"time":1531329060,"precipIntensity":0,"precipProbability":0},{"time":1531329120,"precipIntensity":0,"precipProbability":0},{"time":1531329180,"precipIntensity":0,"precipProbability":0},{"time":1531329240,"precipIntensity":0,"precipProbability":0},{"time":1531329300,"precipIntensity":0,"precipProbability":0},{"time":1531329360,"precipIntensity":0,"precipProbability":0},{"time":1531329420,"precipIntensity":0,"precipProbability":0},{"time":1531329480,"precipIntensity":0,"precipProbability":0},{"time":1531329540,"precipIntensity":0,"precipProbability":0},{"time":1531329600,"precipIntensity":0,"precipProbability":0},{"time":1531329660,"precipIntensity":0,"precipProbability":0},{"time":1531329720,"precipIntensity":0,"precipProbability":0},{"time":1531329780,"precipIntensity":0,"precipProbability":0},{"time":1531329840,"precipIntensity":0,"precipProbability":0},{"time":1531329900,"precipIntensity":0,"precipProbability":0},{"time":1531329960,"precipIntensity":0.003,"precipIntensityError":0.001,"precipProbability":0.01,"precipType":"rain"},{"time":1531330020,"precipIntensity":0,"precipProbability":0},{"time":1531330080,"precipIntensity":0.003,"precipIntensityError":0.001,"precipProbability":0.01,"precipType":"rain"},{"time":1531330140,"precipIntensity":0,"precipProbability":0},{"time":1531330200,"precipIntensity":0.003,"precipIntensityError":0.001,"precipProbability":0.01,"precipType":"rain"},{"time":1531330260,"precipIntensity":0.003,"precipIntensityError":0.001,"precipProbability":0.01,"precipType":"rain"},{"time":1531330320,"precipIntensity":0.003,"precipIntensityError":0.001,"precipProbability":0.01,"precipType":"rain"},{"time":1531330380,"precipIntensity":0,"precipProbability":0},{"time":"

I got confused because during the video it doesn't show this. It looks like its checking the same 3 things over and over again like 100x a second and that seems excessive and really annoying. Can I stop that?

It is showing the minute-by-minute forecast for future minutes starting from the current time. It's not checking anything repeatedly, it is outputting a forecast/prediction for each minute, starting from now for the next hour.

The data is segmented into a section for "currently", i.e. what the weather is doing now. Then it shows a minute-by-minute forecast for what's predicted to happen, next you get an hourly forecast with more metrics and then a daily forecast with even more detail.

To make the JSON page clearer try installing a JSON formatter extension. I use one in Chrome which makes reading the JSON output far easier.

No, you can't "stop that" as it is doing what you asked but what you can do is not use the data you don't want. The app will manage that for you. At the moment, you're just writing to the log so you're getting all the output. Eventually, you'll just use the output you want and the rest will be invisible.

Steve.