Skip to content

Instantly share code, notes, and snippets.

@ericksprengel
Created October 20, 2016 11:21
Show Gist options
  • Save ericksprengel/b182f6ea5ee0954015ff75c65491cb5c to your computer and use it in GitHub Desktop.
Save ericksprengel/b182f6ea5ee0954015ff75c65491cb5c to your computer and use it in GitHub Desktop.
Retrofit - Giphy API
package br.com.ericksprengel.aa.retrofit.model;
import java.util.Map;
public class Giphy {
public String id;
public String type;
public Map<String, GiphyImage> images;
public class GiphyImage {
public String url;
public int width;
public int height;
public int size;
public String mp4;
public int mp4_size;
public String webp;
public int webp_size;
}
}
package br.com.ericksprengel.aa.retrofit.model;
import java.util.List;
public class GiphySearch {
public List<Giphy> data;
public Meta meta;
public Pagination pagination;
class Meta {
public int status;
public String msg;
public String response_id;
}
class Pagination {
public int total_count;
public int count;
public int offset;
}
}
package br.com.ericksprengel.aa.retrofit.api;
import java.util.List;
import br.com.ericksprengel.aa.retrofit.model.GiphySearch;
import br.com.ericksprengel.aa.retrofit.model.Gist;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface GiphyService {
public static final String API_KEY = "_X_X_X_X_X_X_"; // TODO:replace with your API key
// http://api.giphy.com/v1/gifs/search?q=cats&limit=9&api_key=dc6zaTOxFJmzC
@GET("gifs/search")
Call<GiphySearch> searchGifs(@Query("q") String query, @Query("limit") int limit, @Query("api_key") String api_key);
}
package br.com.ericksprengel.aa.retrofit.api;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class GiphyServiceBuilder {
public static GiphyService build() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addNetworkInterceptor(new StethoInterceptor()); // <-- this is the important line!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.giphy.com/v1/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
return retrofit.create(GiphyService.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment