Search in sources :

Example 56 with Retrofit

use of retrofit2.Retrofit in project gce2retrofit by chiuki.

the class Discovery method main.

public static void main(String[] args) {
    Retrofit restAdapter = new Retrofit.Builder().baseUrl(API_URL).addConverterFactory(GsonConverterFactory.create()).build();
    Apis apis = restAdapter.create(Apis.class);
    try {
        Response<DirectoryList> response = apis.list(null, null).execute();
        System.out.println(response.body().kind);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
Also used : Retrofit(retrofit2.Retrofit) Apis(com.googleapis.www.Apis) IOException(java.io.IOException) DirectoryList(com.googleapis.www.model.DirectoryList)

Example 57 with Retrofit

use of retrofit2.Retrofit in project Retrofit-Android-Basic by basil2style.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, final String authToken) {
    if (authToken != null) {
        httpClient.interceptors().add(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();
                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder().header("Authorization", authToken).method(original.method(), original.body());
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }
    OkHttpClient client = httpClient.build();
    Retrofit retrofit = builder.client(client).build();
    return retrofit.create(serviceClass);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor)

Example 58 with Retrofit

use of retrofit2.Retrofit in project BBS-Android by bdpqchen.

the class CollectionClient method loadCollection.

void loadCollection() {
    Retrofit retrofit = new Retrofit.Builder().client(client).addConverterFactory(GsonConverterFactory.create()).baseUrl(RxDoHttpClient.BASE_URL + "home/").build();
    CollectionApi collectionApi = retrofit.create(CollectionApi.class);
    Call<CollectionBean> call = collectionApi.getCollection(uidToken);
    call.enqueue(new Callback<CollectionBean>() {

        @Override
        public void onResponse(Call<CollectionBean> call, Response<CollectionBean> response) {
            if (response != null) {
                collectionPresenter.setCollectionDate(response.body());
            }
        }

        @Override
        public void onFailure(Call<CollectionBean> call, Throwable t) {
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) CollectionBean(com.twtstudio.bbs.bdpqchen.bbs.individual.model.CollectionBean)

Example 59 with Retrofit

use of retrofit2.Retrofit in project BBS-Android by bdpqchen.

the class CollectionClient method collectByTid.

// TODO: 2017/5/23 传入Prenster来处理返回数据
void collectByTid(String tid) {
    Retrofit retrofit = new Retrofit.Builder().client(client).addConverterFactory(GsonConverterFactory.create()).baseUrl("https://bbs.twtstudio.com/api/home/").build();
    CollectionApi collectionApi = retrofit.create(CollectionApi.class);
    Call<SimpleBean> call = collectionApi.collectByTid(uidToken, tid);
    call.enqueue(new Callback<SimpleBean>() {

        @Override
        public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
            collectionPresenter.dealCollectData(response.body());
        }

        @Override
        public void onFailure(Call<SimpleBean> call, Throwable t) {
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) SimpleBean(com.twtstudio.bbs.bdpqchen.bbs.individual.model.SimpleBean)

Example 60 with Retrofit

use of retrofit2.Retrofit in project YourAppIdea by Michenux.

the class MongolabPlaceServiceFactory method create.

public static MongolabPlaceService create(Context context) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer());
    gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
    String url = context.getString(R.string.aroundme_placeremoteprovider_url);
    Retrofit retrofit = new Retrofit.Builder().baseUrl(url).client(client).addConverterFactory(GsonConverterFactory.create(gsonBuilder.create())).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit.create(MongolabPlaceService.class);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) LoggingInterceptor(org.michenux.drodrolib.network.okhttp.LoggingInterceptor) TimestampDeserializer(org.michenux.drodrolib.network.gson.TimestampDeserializer) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) LocationDeserializer(org.michenux.drodrolib.network.gson.LocationDeserializer)

Aggregations

Retrofit (retrofit2.Retrofit)81 Test (org.junit.Test)42 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)35 Before (org.junit.Before)34 MockResponse (okhttp3.mockwebserver.MockResponse)30 ResponseBody (okhttp3.ResponseBody)27 Type (java.lang.reflect.Type)25 IOException (java.io.IOException)22 OkHttpClient (okhttp3.OkHttpClient)22 ParameterizedType (java.lang.reflect.ParameterizedType)13 MediaType (okhttp3.MediaType)13 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Gson (com.google.gson.Gson)11 GsonBuilder (com.google.gson.GsonBuilder)11 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)11 Converter (retrofit2.Converter)10 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)10 Annotation (java.lang.annotation.Annotation)9