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 Working with JSON Cleaning Up the Date and Time

was following the weather app tutorial and i had an error in the main activity (cannot find symbol method setHumidity)

package com.example.student.stormy;

import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.textclassifier.TextLinks; 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 currentWeather;


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

    String apiKey = "3fe2d30e42ece3b7f28765fb2483e5d6";
    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()
                .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 {
                    String jsonData = response.body().string();
                    // Response response = call.execute();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful()) {


                        currentWeather = getCurrentDetails(jsonData);

                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception caught: ", e);
                }catch (JSONException e){
                    Log.e(TAG, "JSON Exception caught", e);
                }
            }
        });
    }

    Log.d(TAG, "Main UI is running, horray");

}

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{
    JSONObject forecast = new JSONObject(jsonData);

    String timezone = forecast.getString("timezone");
    Log.i(TAG, "from JSON: " + timezone);

    JSONObject currently = forecast.getJSONObject("currently");

    CurrentWeather currentWeather = new CurrentWeather();

    currentWeather.setHumidity(currently.getDouble("humidity"));
    currentWeather.setTime(currently.getLong("time"));
    currentWeather.setIcon(currently.getString("icon"));
    currentWeather.setLocationLabel("Alcatraz Island, CA");
    currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
    currentWeather.setSummary(currently.getString("summary"));
    currentWeather.setTemperature(currently.getDouble("temperature"));

    return 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;
    }
    else{
        Toast.makeText(this, R.string.network_unavailable_message,
                Toast.LENGTH_LONG).show();
    }
    return isAvailable;
}

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

}

1 Answer

Your just need to add the humidity field to the CurrentWeather Class...

private double humidity;

then use Android studio to create your Getter and Setter for it by

1 - Placing your caret at anywhere on the field "humidity"

2 - Use the light bulb to select "Create Getter and Setter for humidity" option.

It will generate these methods for the field...

 public double getHumidity() {
    return humidity;
}

public void setHumidity(double humidity) {
    this.humidity = humidity;
}