Skip to content

Instantly share code, notes, and snippets.

@derohimat
Last active April 9, 2016 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derohimat/6e18ee2f14f5e556c648448517a3839d to your computer and use it in GitHub Desktop.
Save derohimat/6e18ee2f14f5e556c648448517a3839d to your computer and use it in GitHub Desktop.
Gist Sunshine MainFragment
private void loadData() {
mWeekForecastAdapter.clear();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String location = sharedPreferences.getString(getString(R.string.pref_location_key),
getString(R.string.pref_location_default));
String units = sharedPreferences.getString(getString(R.string.pref_units_key),
getString(R.string.pref_units_metric));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SimpleService.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
SimpleService.OpenWeatherMap service = retrofit
.create(SimpleService.OpenWeatherMap.class);
Call<ApiResponse> listCall = service.getWeather(location, units,
7, "YOUR_API_KEY");
listCall.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call,
Response<ApiResponse> response) {
if (response.isSuccessful()){
ApiResponse apiResponse = response.body();
for (int i = 0; i < apiResponse.getList().size(); i++) {
Weather weather = apiResponse.getList().get(i);
String weatherString = weather.getWeather().get(0).getMain()
+ " - " + weather.getTemp().getMax();
weekForecastString.add(weatherString);
}
mWeekForecastAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ApiResponse> call,
Throwable t) {
Toast.makeText(mContext, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment