Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Weather App (2015) Working with JSON Introducing JSONObject

Caleb Seeling
Caleb Seeling
1,189 Points

Cant get Log(Data from forecast)

I dont get the Data from forecast and when I look into the Logcat I can only see the OpenGLRenderer failed to config this: EGL_SWAP_BEHAVIOR_PRESERVED.

Code: Main Activity

package com.example.calebseeling.surfy;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.net.ConnectivityManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

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

private CurrentWeather mCurrentWeather;

    protected void onCreate(Bundle savedInstanceState) {
        String apiKey = "**HIDDEN BY MOD**";
        double latitude =37.8267 ;
        double longitude = -122.4233;
        String forecasturl ="https://api.darksky.net/forecast/" + apiKey +
                "/" + latitude + "," + longitude;

        if (isNetworkAvailable()) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()

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


                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        String jsonData = response.body().string();
                        Log.v(Tag, jsonData );
                        if (response.isSuccessful()) {
                            mCurrentWeather = getCurrentDetails(jsonData);
                        } else {

                    } catch (IOException e) {
                        Log.e(Tag, "Exception caught: ", e);
                    catch (JSONException e){
                        Log.e(Tag, "Exception caught: ", e);


        else {
            Toast.makeText(this, "Network is unavailable", Toast.LENGTH_SHORT).show();

        Log.d(Tag,"Main UI Code is running");


    private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");
        Log.i(Tag, "From JSON:" + timezone);
        return new CurrentWeather();

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo  networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        return isAvailable;

    private void alertUserAboutError() {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");


Code: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Code: CurrentWeather

package com.example.calebseeling.surfy;

 * Created by calebseeling on 24.12.17.

public class CurrentWeather {
    private int mIcon;
    private long mTime;
    private double mTemperature;
    private double mHumidity;
    private double mPrecipChance;

    public int getIcon() {
        return mIcon;

    public void setIcon(int icon) {
        mIcon = icon;

    public long getTime() {
        return mTime;

    public void setTime(long time) {
        mTime = time;

    public double getTemperature() {
        return mTemperature;

    public void setTemperature(double temperature) {
        mTemperature = temperature;

    public double getHumidity()  {
        return mHumidity;

    public void setHumidity(double humidity) {
        mHumidity = humidity;

    public double getPrecipChance() {
        return mPrecipChance;

    public void setPrecipChance(double precipChance) {
        mPrecipChance = precipChance;

[MOD: edited code blocks and hid API key - srh]

Could you post the logcat to please.