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
niveditagautam
Courses Plus Student 3,001 PointsNot able to get the json data ?? App shows the error box ??
Here is the code from MainActivity.java
package nivedita.stormy;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
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 = "API key";
double latitude = 37.8267;
double longitude = -122.423;
String forecastURL = "https://api.forecast.io/forecast/"+APIkey+"/"+latitude+","+longitude;
if(isNetworkAvailable())
{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastURL)
.build();
client.newCall(request).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.d(TAG, "JSON data is: " + jsonData);
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);
}
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);
mCurrentWeather.setTimeZone(timezone);
Log.d(TAG,mCurrentWeather.getFormattedTime());
return mCurrentWeather;
}
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");
}
}
And here is the complete log:
10-29 02:24:17.360 839-839/nivedita.stormy D/MainActivity﹕ Main UI code is running!
10-29 02:24:18.120 839-839/nivedita.stormy D/﹕ HostConnection::get() New Host Connection established 0xb8307730, tid 839
10-29 02:24:18.650 839-839/nivedita.stormy W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-29 02:24:18.670 839-839/nivedita.stormy D/OpenGLRenderer﹕ Enabling debug mode 0
10-29 02:24:22.100 839-852/nivedita.stormy D/dalvikvm﹕ GC_FOR_ALLOC freed 264K, 14% free 3409K/3956K, paused 56ms, total 59ms
10-29 02:24:23.470 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
10-29 02:24:23.510 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
10-29 02:24:23.510 839-852/nivedita.stormy I/dalvikvm﹕ Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
10-29 02:24:23.510 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to resolve static method 1822: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
10-29 02:24:23.520 839-852/nivedita.stormy D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
10-29 02:24:23.630 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
10-29 02:24:23.630 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
10-29 02:24:23.640 839-852/nivedita.stormy I/dalvikvm﹕ Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
10-29 02:24:23.640 839-852/nivedita.stormy W/dalvikvm﹕ VFY: unable to resolve static method 1821: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
10-29 02:24:23.640 839-852/nivedita.stormy D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
10-29 02:24:26.360 839-852/nivedita.stormy D/dalvikvm﹕ GC_FOR_ALLOC freed 353K, 16% free 3568K/4204K, paused 52ms, total 54ms
10-29 02:24:26.690 839-852/nivedita.stormy I/MainActivity﹕ From JSON: America/Los_Angeles
10-29 02:24:26.700 839-852/nivedita.stormy W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1b0eba8)
10-29 02:24:26.720 839-852/nivedita.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: OkHttp Dispatcher
Process: nivedita.stormy, PID: 839
java.lang.NullPointerException
at nivedita.stormy.MainActivity.getCurrentDetails(MainActivity.java:99)
at nivedita.stormy.MainActivity.access$100(MainActivity.java:22)
at nivedita.stormy.MainActivity$1.onResponse(MainActivity.java:63)
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:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
10-29 02:24:34.270 839-839/nivedita.stormy I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
10-29 02:25:50.460 1105-1105/nivedita.stormy D/MainActivity﹕ Main UI code is running!
10-29 02:25:50.890 1105-1105/nivedita.stormy D/﹕ HostConnection::get() New Host Connection established 0xb8306760, tid 1105
10-29 02:25:51.090 1105-1105/nivedita.stormy W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-29 02:25:51.110 1105-1105/nivedita.stormy D/OpenGLRenderer﹕ Enabling debug mode 0
10-29 02:25:52.410 1105-1118/nivedita.stormy D/dalvikvm﹕ GC_FOR_ALLOC freed 266K, 14% free 3407K/3956K, paused 53ms, total 56ms
10-29 02:25:53.210 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
10-29 02:25:53.210 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
10-29 02:25:53.220 1105-1118/nivedita.stormy I/dalvikvm﹕ Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
10-29 02:25:53.220 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to resolve static method 1821: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
10-29 02:25:53.220 1105-1118/nivedita.stormy D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
10-29 02:25:53.220 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
10-29 02:25:53.230 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
10-29 02:25:53.260 1105-1118/nivedita.stormy I/dalvikvm﹕ Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
10-29 02:25:53.260 1105-1118/nivedita.stormy W/dalvikvm﹕ VFY: unable to resolve static method 1820: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
10-29 02:25:53.260 1105-1118/nivedita.stormy D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
10-29 02:25:54.420 1105-1118/nivedita.stormy D/MainActivity﹕ JSON data is: {"latitude":37.8267,"longitude":-122.423,"timezone":"America/Los_Angeles","offset":-7,"currently":{"time":1446099953,"summary":"Clear","icon":"clear-night","nearestStormDistance":0,"precipIntensity":0,"precipProbability":0,"temperature":60.21,"apparentTemperature":60.21,"dewPoint":54.72,"humidity":0.82,"windSpeed":7.48,"windBearing":322,"visibility":6.69,"cloudCover":0.02,"pressure":1016.2,"ozone":271.1},"minutely":{"summary":"Clear for the hour.","icon":"clear-night","data":[{"time":1446099900,"precipIntensity":0,"precipProbability":0},{"time":1446099960,"precipIntensity":0,"precipProbability":0},{"time":1446100020,"precipIntensity":0,"precipProbability":0},{"time":1446100080,"precipIntensity":0,"precipProbability":0},{"time":1446100140,"precipIntensity":0,"precipProbability":0},{"time":1446100200,"precipIntensity":0,"precipProbability":0},{"time":1446100260,"precipIntensity":0,"precipProbability":0},{"time":1446100320,"precipIntensity":0,"precipProbability":0},{"time":1446100380,"precipIntensity":0,"precipProbability":0},{"time":1446100440,"precipIntensity":0,"precipProbability":0},{"time":1446100500,"precipIntensity":0,"precipProbability":0},{"time":1446100560,"precipIntensity":0,"precipProbability":0},{"time":1446100620,"precipIntensity":0,"precipProbability":0},{"time":1446100680,"precipIntensity":0,"precipProbability":0},{"time":1446100740,"precipIntensity":0,"precipProbability":0},{"time":1446100800,"precipIntensity":0,"precipProbability":0},{"time":1446100860,"precipIntensity":0,"precipProbability":0},{"time":1446100920,"precipIntensity":0,"precipProbability":0},{"time":1446100980,"precipIntensity":0,"precipProbability":0},{"time":1446101040,"precipIntensity":0,"precipProbability":0},{"time":1446101100,"precipIntensity":0,"precipProbability":0},{"time":1446101160,"precipIntensity":0,"precipProbability":0},{"time":1446101220,"precipIntensity":0,"precipProbability":0},{"time":1446101280,"precipIntensity":0,"precipProbability":0},{"time":1446101340,"precipIntensity":0,"precipProbability":0},{"time":1446101400,"precipIntensity":0,"precipProbability":0},{"time":1446101460,"precipIntensity":0,"precipProbability":0},{"time":1446101520,"precipIntensity":0,"precipProbability":0},{"time":1446101580,"precipIntensity":0,"precipProbability":0},{"time":1446101640,"precipIntensity":0,"precipProbability":0},{"time":1446101700,"precipIntensity":0,"precipProbability":0},{"time":1446101760,"precipIntensity":0,"precipProbability":0},{"time":1446101820,"precipIntensity":0,"precipProbability":0},{"time":1446101880,"precipIntensity":0,"precipProbability":0},{"time":1446101940,"precipIntensity":0,"precipProbability":0},{"time":1446102000,"precipIntensity":0,"precipProbability":0},{"time":1446102060,"precipIntensity":0,"precipProbability":0},{"time":1446102120,"precipIntensity":0,"precipProbability":0},{"time":1446102180,"precipIntensity":0,"precipProbability":0},{"time":1446102240,"precipIntensity":0,"precipProbability":0},{"time":1446102300,"precipIntensity":0,"precipProbability":0},{"time":1446102360,"precipIntensity":0,"precipProbability":0},{"time":1446102420,"precipIntensity":0,"precipProbability":0},{"time":1446102480,"precipIntensity":0,"precipProbability":0},{"time":1446102540,"precipIntensity":0,"precipProbability":0},{"time":1446102600,"precipIntensity":0,"precipProbability":0},{"time":1446102660,"precipIntensity":0,"precipProbability":0},{"time":1446102720,"precipIntensity":0,"precipProbability":0},{"time":1446102780,"precipIntensity":0,"precipProbability":0},{"time":1446102840,"precipIntensity":0,"precipProbability":0},{"time":1446102900,"precipIntensity":0,"precipProbability":0},{"time":1446102960,"precipIntensity":0,"precipProbability":0},{"time":1446103020,"precipIntensity":0,"precipProbability":0},{"time":1446103080,"precipIntensity":0,"precipProbability":0},{"time":1446103140,"precipIntensity":0,"precipProbability":0},{"time":1446103200,"precipIntensity":0,"precipProbability":0},{"time":1446103260,"precipIntensity":0,"precipProbability":0},{"time":1446103320,"precipIntens
10-29 02:25:54.980 1105-1118/nivedita.stormy D/dalvikvm﹕ GC_FOR_ALLOC freed 369K, 16% free 3552K/4204K, paused 44ms, total 46ms
10-29 02:25:55.440 1105-1118/nivedita.stormy I/MainActivity﹕ From JSON: America/Los_Angeles
10-29 02:25:55.440 1105-1118/nivedita.stormy W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1b0eba8)
10-29 02:25:55.460 1105-1118/nivedita.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: OkHttp Dispatcher
Process: nivedita.stormy, PID: 1105
java.lang.NullPointerException
at nivedita.stormy.MainActivity.getCurrentDetails(MainActivity.java:99)
at nivedita.stormy.MainActivity.access$100(MainActivity.java:22)
at nivedita.stormy.MainActivity$1.onResponse(MainActivity.java:63)
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:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Here is the CurrentWeather.java code:
package nivedita.stormy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class CurrentWeather {
private String mIcon;
private long mTime;
private double mTemperature;
private double mHumidity;
private double mPercipChance;
private String mSummary;
private String mTimeZone;
public String getTimeZone() {
return mTimeZone;
}
public void setTimeZone(String timeZone) {
mTimeZone = timeZone;
}
public String getFormattedTime()
{
SimpleDateFormat formatter = new SimpleDateFormat("h:mm");
formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
Date dateTime = new Date(getTime()*1000);
String timeString = formatter.format(dateTime);
return timeString;
}
public String getIcon() {
return mIcon;
}
public void setIcon(String icon) {
mIcon = icon;
}
public int getIconID()
{
//icon: "partly-cloudy-night"
int iconId = R.drawable.clear_day;
if (mIcon.equals("clear-day")) {
iconId = R.drawable.clear_day;
}
else if (mIcon.equals("clear-night")) {
iconId = R.drawable.clear_night;
}
else if (mIcon.equals("rain")) {
iconId = R.drawable.rain;
}
else if (mIcon.equals("snow")) {
iconId = R.drawable.snow;
}
else if (mIcon.equals("sleet")) {
iconId = R.drawable.sleet;
}
else if (mIcon.equals("wind")) {
iconId = R.drawable.wind;
}
else if (mIcon.equals("fog")) {
iconId = R.drawable.fog;
}
else if (mIcon.equals("cloudy")) {
iconId = R.drawable.cloudy;
}
else if (mIcon.equals("partly-cloudy-day")) {
iconId = R.drawable.partly_cloudy;
}
else if (mIcon.equals("partly-cloudy-night")) {
iconId = R.drawable.cloudy_night;
}
return iconId;
}
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 getPercipChance() {
return mPercipChance;
}
public void setPercipChance(double percipChance) {
mPercipChance = percipChance;
}
public String getSummary() {
return mSummary;
}
public void setSummary(String summary) {
mSummary = summary;
}
}
1 Answer
Harry James
14,780 PointsHey Nivedita!
It looks like your API request URL is malformed, here's what it should look like:
String forecastURL = "https://api.forecast.io/forecast/" + APIkey + "/" + latitude + "," + longitude;
(Note: I'm assuming you just replaced your actual API Key with "API key" so that it is not declared publicly on the post. Your code should obviously use your real key!)
This should be enough to get past the error. Just a heads up though, you have written some of your code as a String for your TAG variable. It should look like this:
public static final String TAG = MainActivity.class.getSimpleName()";
Hope it helps and rock on with the project :) If you run into any more problems or need anything explained, feel free to give me a shout ;)
niveditagautam
Courses Plus Student 3,001 Pointsniveditagautam
Courses Plus Student 3,001 PointsOh thanks! Lemme check that!
niveditagautam
Courses Plus Student 3,001 Pointsniveditagautam
Courses Plus Student 3,001 PointsHi again ! I fixed that, but now I am getting a NullPointerException:
on :
I tried changing:
return new CurrentWeather();to:
return mCurrentWeather;I saw this answer here: http://stackoverflow.com/questions/30900695/fatal-exception-main-android-null-pointer-exception
It says problem is here:
formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));How do I fix this?
I updated the question with CurrentWeather.java code.
Harry James
14,780 PointsHarry James
14,780 PointsHey again Nivedita!
In the onResponse() callback's try method, can you change your log statement from this:
Log.v(TAG, jsonData);to:
and provide another full Logcat with the Filter view set to Debug to help me see what's going wrong here?
Also while you're at it, it would help if you provided updated code files.
Speak to you soon :)
niveditagautam
Courses Plus Student 3,001 Pointsniveditagautam
Courses Plus Student 3,001 PointsHey !
I have updated the code and the question like you said. Can you take a look ?
Harry James
14,780 PointsHarry James
14,780 PointsHey again Nivedita!
It looks like your CurrentWeather variable is null, it hasn't been set anywhere. You're trying to call
setTimeZone()off a null reference, which is giving you the error.To solve this, update your
mCurrentWeathermember variable by initializing it as follows:private CurrentWeather mCurrentWeather = new CurrentWeather();Hope it helps and if there's still more errors after this (Hopefully not!) then send a new logcat in and I'll get back to you :)