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 (2015) Working with JSON Setting CurrentWeather from JSON

getting EXEPTION CATUGHT error

EXEPTION CATUGHT org.json.JSONException: No value for tmezone at org.json.JSONObject.get(JSONObject.java:389) at org.json.JSONObject.getString(JSONObject.java:550) at com.sankalp.weathergo.MainActivity.getCurrentDetail(MainActivity.java:79) at com.sankalp.weathergo.MainActivity.access$100(MainActivity.java:22) at com.sankalp.weathergo.MainActivity$1.onResponse(MainActivity.java:56) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:126) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

code:-- public class MainActivity extends AppCompatActivity { public static final String Tag = MainActivity.class.getSimpleName(); private CurrentWeather mCurrentWeather;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String apiKey = "-------------------";
    double lat = 37.8267;
    double log = -122.4233;
    String forcastUrl = "https://api.darksky.net/forecast/" + apiKey + "/" + lat + "," + log;

    if (isNetworkAvailabe()) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(forcastUrl)
                .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();
                    Log.v(Tag, response.body().toString());
                    if (response.isSuccessful()) {
                        mCurrentWeather = getCurrentDetail(jsonData);
                    } else {
                        alertuserAboutUser();
                    }
                }
                catch (IOException | JSONException e)
                {
                    Log.e(Tag,"EXEPTION CATUGHT",e);
                }
            }

        });
    }else {
        Toast.makeText(this, R.string.networkUnavailable,Toast.LENGTH_LONG).show();
    }

    Log.d(Tag, "MAIN UI");


}

private CurrentWeather getCurrentDetail(String jsonData)  throws JSONException{
        JSONObject forcast = new JSONObject(jsonData);
    String timeZone = forcast.getString("tmezone");
            Log.i(Tag,"From json" + timeZone);
    JSONObject currently  = forcast.getJSONObject("currently");
  CurrentWeather currentWeather=new CurrentWeather();
     currentWeather.setmHum(currently.getDouble("humidity"));
     currentWeather.setmTime(currently.getLong("time"));
     currentWeather.setmIcon(currently.getString("icon"));
     currentWeather.setmPreChance(currently.getDouble("precipProbability"));
     currentWeather.setmSummry(currently.getString("summary"));
     currentWeather.setmTem(currently.getDouble("temperature"));

    return  new CurrentWeather();



}

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

private void alertuserAboutUser() {
    AlertDilogFragment dilog =new AlertDilogFragment();
    dilog.show(getFragmentManager(),"error Dilog");
}

1 Answer

Hi. You have misspelled timezone. Your error message shows you missed an "i" and spelled tmezone. It should be

String timezone = forecast.getString("timezone");