Skip to content

Instantly share code, notes, and snippets.

@0x5d
Last active October 8, 2015 13:23
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 0x5d/a4ea66ecae3577ee98de to your computer and use it in GitHub Desktop.
Save 0x5d/a4ea66ecae3577ee98de to your computer and use it in GitHub Desktop.
package com.mycompany.myfirstapp.activity.weather;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import com.mycompany.myfirstapp.service.WeatherInfoService;
import com.mycompany.myfirstapp.model.weather.WeatherInfo;
import retrofit.Callback;
import retrofit.Response;
public class WeatherActivity extends AppCompatActivity {
// This will hold our WeatherInfoService instance.
private WeatherInfoService weatherInfoService;
@Override
protected void onCreate(Bundle savedInstanceState) {
// onCreate logic...
// Initialize our service instance.
weatherInfoService = new WeatherInfoService();
}
private void getWeatherForName(String name) {
try {
weatherInfoService.getWeatherForName(name, new Callback() {
// This is executed when the request goes well :)
@Override
public void onResponse(Response response) {
// response.body holds the response, mapped to our model.
WeatherInfo weatherInfo = (WeatherInfo) response.body();
// Do something with weatherInfo, like
// String description = weatherInfo.getWeather()[0].getDescription();
// which will be something like "Sky is Clear".
}
// This one's called when it goes bad :(
@Override
public void onFailure(Throwable t) {
// Tell the user there was an error.
}
});
} catch (IOException e) {
// Recover from failure.
}
}
private void getWeatherForCoords(double lat, double lon) {
try {
weatherInfoService.getWeatherForCoords(lat, lon, new Callback() {
@Override
public void onResponse(Response response) {
// here, too, response.body holds the response, mapped to our model.
WeatherInfo weatherInfo = (WeatherInfo) response.body();
// Do something with it. Or not. But that would be weird.
}
@Override
public void onFailure(Throwable t) {
// Tell the user something went wrong.
}
});
} catch (IOException e) {
// Recover from failure!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment