//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());
}
});
//**********************************************//
//En KOTLIN
interface API
{
@GET("getCurrent.php")
fun getCurrent(): Call<CurrentPrice>;
}
//RestBase
class RestBase
{
init {
setupRestBase()
}
fun getApiService(): API?
{
return REST_CLIENT
}
private val BASE_URL = "http://owncloud.redirectme.net/apps/"
private var REST_CLIENT: API? = null;
fun setupRestBase()
{
var okHttpClient = getHttpClient().build()
var retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
REST_CLIENT = retrofit.create(API::class.java)
}
fun getHttpClient(): OkHttpClient.Builder
{
var httpClient = OkHttpClient.Builder()
httpClient.addInterceptor({chain: Interceptor.Chain? ->
var original = chain?.request()
var request = original?.newBuilder()
?.header("Accept", "application/json")
?.header("Authorization", "auth-token")
?.method(original?.method(), original?.body())
?.build()
var response = chain?.proceed(request)
return@addInterceptor response
})
return httpClient
}
}
fun <T> callback(success: ((retrofit2.Response<T>) -> Unit)?, failure: ((throwable: Throwable) -> Unit)? = null): Callback<T> {
return object : Callback<T> {
override fun onResponse(call: Call<T>, response: retrofit2.Response<T>) { success?.invoke(response) }
override fun onFailure(call: Call<T>, throwable: Throwable) { failure?.invoke(throwable) }
}
}
//Uso
var call = RestBase().getApiService()?.getCurrent();
call?.enqueue(callback(
{response ->
Log.d("APP", "Consulta correcta: " + response.body()?.precio)
},
{throwable ->
Log.d("APP", "Consulta fallida: " + throwable.message)
}
))
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.