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 triallaze dimitrovski
1,535 PointsDoesn't seem to work
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
Current mCurrent;
@BindView(R.id.tempTextView) TextView mTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APIKey = "79c776e9f419481bbe3ecbbb6f6099c8";
double latitude = 41.1421743;
double longtitiude = 22.4851028;
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority("api.forecast.io")
.appendPath("forecast")
.appendPath(APIKey)
.appendPath(latitude + "," + longtitiude)
.appendQueryParameter("units", "si");
String URL = builder.build().toString();
Log.i(TAG, URL);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.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 {
String jsonData = response.body().string();
if(response.isSuccessful()){
try {
mCurrent = getData(jsonData);
} catch (JSONException e) {
e.printStackTrace();
}
}else{
Error();
}
}
});
}
private void Error(){
Toast.makeText(this , "NO NET", Toast.LENGTH_SHORT).show();
}
private Current getData(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
JSONObject currently = forecast.getJSONObject("currently");
Current now = new Current();
now.setTemp(currently.getDouble("temperature"));
now.setPrecip(currently.getDouble("precipProbability"));
now.setSummary(currently.getString("summary"));
now.setTime(currently.getLong("time"));
Log.i(TAG, now.getSummary());
return now;
}
}
I can't get mCurrent to not be null.
1 Answer
laze dimitrovski
1,535 PointsI fixed it. The error was, I was calling "return now;" while I should've called "return Current;"
J.D. Sandifer
18,813 PointsJ.D. Sandifer
18,813 PointsCould you post your code for the Current class, also?