Skip to content

Instantly share code, notes, and snippets.

@rohans-doordash
Last active June 29, 2017 20:07
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 rohans-doordash/c63033d011bd498317d68378d0753504 to your computer and use it in GitHub Desktop.
Save rohans-doordash/c63033d011bd498317d68378d0753504 to your computer and use it in GitHub Desktop.
Fetch restaurant list at a user's default address using Retrofit
/**
* Fetch the current user's default address
*/
public void getUserInfo(int userId) {
ddApi.getUserInfo(userId, new Callback<User>() {
@Override
public void onResponse(@NonNull Call<User> call,
@NonNull Response<User> response) {
if (response.isSuccessful() && response.body() != null) {
User user = response.body();
getRestaurants(user.defaultAddress.lat, user.defaultAddress.lng);
} else {
// display the error message contained in response.errorBody()
}
}
@Override
public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
// display a network error message
}
});
}
/**
* Fetch restaurants at the given location
*/
public void getRestaurants(double lat, double lng) {
ddApi.getRestaurants(lat, lng, new Callback<List<Restaurant>>() {
@Override
public void onResponse(@NonNull Call<List<Restaurant>> call,
@NonNull Response<List<Restaurant>> response) {
if (response.isSuccessful() && response.body() != null) {
// update adapter with restaurants
} else {
// display the error message contained in response.errorBody()
}
}
@Override
public void onFailure(@NonNull Call<List<Restaurant>> call, @NonNull Throwable t) {
// display a network error message
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment