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 trialOrla McGreal
1,625 PointsI get this error in my Log: JSONException: No value for Summary. Does anyone know how to fix it?
Here is the full error : org.json.JSONException: No value for Summary at org.json.JSONObject.get(JSONObject.java:354) at org.json.JSONObject.getString(JSONObject.java:514) at com.onebackforlife.orla.stormy.MainActivity.getCurrentDetails(MainActivity.java:121) at com.onebackforlife.orla.stormy.MainActivity.access$100(MainActivity.java:28) at com.onebackforlife.orla.stormy.MainActivity$1.onResponse(MainActivity.java:77) at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168) at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841)
Here is MainActivity - But I don't get where the error is coming from...
public class MainActivity extends Activity {
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 = "64fd6c2dd5fb383c62e0e523f7b27fd9";
double latitude = 37.8267;
double longitude = -122.423;
String forecastURL = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
/// network issues
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
/// clients request for the info
Request request = new Request.Builder()
.url(forecastURL)
.build();
// putting the request into the call
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
// the response
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
} else {
alertUserAboutError();
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught:", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught:", e);
}
}
});
}
else {
Toast.makeText(this, getString(R.string.network_unavailable_message),
Toast.LENGTH_LONG).show();
}
Log.d(TAG, "Main UI Code is running!");
}
// throwing an exception to where you want it
private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
Log.i(TAG, "From JSON:" + timezone);
///// setting a new json object for the currently key
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.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;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
}
2 Answers
Seth Kroger
56,414 PointsWhen you use currently.getString("Summary")
summary need to be all lowercase.
AGHA ALI
Courses Plus Student 136 PointsI was having same issue...Thank You so much!
Orla McGreal
1,625 PointsOrla McGreal
1,625 PointsHi Seth , thanks for helping me so quickly !