Search in sources :

Example 1 with retrofit2.http

use of retrofit2.http in project Tusky by Vavassor.

the class BaseActivity method disablePushNotifications.

protected void disablePushNotifications() {
    Callback<ResponseBody> callback = new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                pushNotificationClient.unsubscribeToTopic(getPushNotificationTopic());
            } else {
                onDisablePushNotificationsFailure();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            onDisablePushNotificationsFailure();
        }
    };
    String deviceToken = pushNotificationClient.getDeviceToken();
    Session session = new Session(getDomain(), getAccessToken(), deviceToken);
    tuskyApi.unregister(session).enqueue(callback);
}
Also used : Response(okhttp3.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) ResponseBody(okhttp3.ResponseBody) Session(com.keylesspalace.tusky.entity.Session)

Example 2 with retrofit2.http

use of retrofit2.http in project Tusky by Vavassor.

the class BaseActivity method enablePushNotifications.

protected void enablePushNotifications() {
    Callback<ResponseBody> callback = new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                pushNotificationClient.subscribeToTopic(getPushNotificationTopic());
                pushNotificationClient.connect(BaseActivity.this);
            } else {
                onEnablePushNotificationsFailure(response.message());
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            onEnablePushNotificationsFailure(t.getMessage());
        }
    };
    String deviceToken = pushNotificationClient.getDeviceToken();
    Session session = new Session(getDomain(), getAccessToken(), deviceToken);
    tuskyApi.register(session).enqueue(callback);
}
Also used : Response(okhttp3.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) ResponseBody(okhttp3.ResponseBody) Session(com.keylesspalace.tusky.entity.Session)

Example 3 with retrofit2.http

use of retrofit2.http in project azure-sdk-for-java by Azure.

the class ResourceUtilsTests method canDownloadFile.

@Test
public void canDownloadFile() throws Exception {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://microsoft.com").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    byte[] content = Utils.downloadFileAsync("http://google.com/humans.txt", retrofit).toBlocking().single();
    String contentString = new String(content);
    Assert.assertNotNull(contentString);
}
Also used : Retrofit(retrofit2.Retrofit) Test(org.junit.Test)

Example 4 with retrofit2.http

use of retrofit2.http in project muzei by romannurik.

the class FiveHundredPxExampleArtSource method onTryUpdate.

@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
    String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(final Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
    FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
    PhotosResponse response;
    try {
        response = service.getPopularPhotos().execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Error reading 500px response", e);
        throw new RetryException();
    }
    if (response == null || response.photos == null) {
        throw new RetryException();
    }
    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }
    Random random = new Random();
    Photo photo;
    String token;
    while (true) {
        photo = response.photos.get(random.nextInt(response.photos.size()));
        token = Integer.toString(photo.id);
        if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
            break;
        }
    }
    publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
    scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Artwork(com.google.android.apps.muzei.api.Artwork) Request(okhttp3.Request) Photo(com.example.muzei.examplesource500px.FiveHundredPxService.Photo) Intent(android.content.Intent) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Retrofit(retrofit2.Retrofit) Random(java.util.Random) PhotosResponse(com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse) Interceptor(okhttp3.Interceptor)

Example 5 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method buildRequest.

static <T> Request buildRequest(Class<T> cls, Object... args) {
    final AtomicReference<Request> requestRef = new AtomicReference<>();
    okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {

        @Override
        public okhttp3.Call newCall(Request request) {
            requestRef.set(request);
            throw new UnsupportedOperationException("Not implemented");
        }
    };
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(new ToStringConverterFactory()).callFactory(callFactory).build();
    Method method = TestingUtils.onlyMethod(cls);
    //noinspection unchecked
    ServiceMethod<T, Call<T>> serviceMethod = (ServiceMethod<T, Call<T>>) retrofit.loadServiceMethod(method);
    Call<T> okHttpCall = new OkHttpCall<>(serviceMethod, args);
    Call<T> call = serviceMethod.callAdapter.adapt(okHttpCall);
    try {
        call.execute();
        throw new AssertionError();
    } catch (UnsupportedOperationException ignored) {
        return requestRef.get();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}
Also used : Request(okhttp3.Request) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) IOException(java.io.IOException) POST(retrofit2.http.POST) PUT(retrofit2.http.PUT) GET(retrofit2.http.GET) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory)

Aggregations

Test (org.junit.Test)82 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)39 OkHttpClient (okhttp3.OkHttpClient)28 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8