//Interface
public interface API
{
@GET("getCurrent")
Call<CurrentPrice> getCurrent();
}
//Integración con OkHttpClient
public class RestBase
{
private static final String BASE_URL = "http://...";
private static API REST_CLIENT;
static {
setupRestBase();
}
private RestBase() {}
public static API getApiService() { return REST_CLIENT; }
public static void setupRestBase()
{
OkHttpClient client = getHttpClient().build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
REST_CLIENT = retrofit.create(API.class);
}
private static OkHttpClient.Builder getHttpClient()
{
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/json")
.header("Authorization", "auth-token")
.method(original.method(), original.body())
.build();
Response response = chain.proceed(request);
return response;
}
});
return httpClient;
}
}
//Uso
/*Call<CurrentPrice> call = RestBase.getApiService().getCurrent();
call.enqueue(new Callback<CurrentPrice>() {
@Override
public void onResponse(retrofit2.Call<CurrentPrice> call, Response<CurrentPrice> response) {
Log.d("APP", "Consulta correcta: " + response.body());
}
@Override
public void onFailure(retrofit2.Call<CurrentPrice> call, Throwable t) {
Log.d("APP", "Consulta fallida " + t.getMessage());
}
});*/
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.