Last active
April 9, 2016 04:49
-
-
Save derohimat/6e18ee2f14f5e556c648448517a3839d to your computer and use it in GitHub Desktop.
Gist Sunshine MainFragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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