Search in sources :

Example 91 with Callback

use of retrofit2.Callback in project Gladys-Android-App by LeptitGeek.

the class TimelineFragment method createEvent.

public void createEvent(final String name) {
    SharedPreferences pref1 = PreferenceManager.getDefaultSharedPreferences(getContext());
    pref_house = pref1.getString("idhouse", "1");
    pref_user = pref1.getString("iduser", "1");
    getConnection();
    if (connection) {
        getCode(name);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).client(SelfSigningClientBuilder.getUnsafeOkHttpClient()).build();
        RetrofitAPI service = retrofit.create(RetrofitAPI.class);
        Call<Event> call = service.createEvents(code, pref_house, pref_user, pref_token);
        call.enqueue(new Callback<Event>() {

            @Override
            public void onResponse(Call<Event> call, Response<Event> response) {
                if (response.code() == 201) {
                    getAllEvents();
                    if (getActivity() != null) {
                        SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.event_created));
                    }
                } else {
                    if (getActivity() != null) {
                        SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.error_code_4));
                    }
                }
            }

            @Override
            public void onFailure(Call<Event> call, Throwable t) {
                if (getActivity() != null) {
                    SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.error_code_4));
                }
            }
        });
    }
}
Also used : Retrofit(retrofit2.Retrofit) SharedPreferences(android.content.SharedPreferences) RetrofitAPI(com.gladysinc.gladys.Utils.RetrofitAPI) SelfSigningClientBuilder(com.gladysinc.gladys.Utils.SelfSigningClientBuilder) Event(com.gladysinc.gladys.Models.Event)

Example 92 with Callback

use of retrofit2.Callback in project syndesis by syndesisio.

the class KubernetesSupport method watchLog.

/*
     * Feeds the controller of the given podName to the callback handler for processing.
     *
     * We do this instead of using the watchLog() feature of the k8s client lib because it really sucks due to:
     *  1. You can't configure the timestamps option or the sinceTime option.  Need to resume log downloads.
     *  2. It seems to need extra threads..
     *  3. It might be hiding some of the http failure conditions.
     *
     */
protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime, Executor executor) throws IOException {
    try {
        PodOperationsImpl pod = (PodOperationsImpl) client.pods().withName(podName);
        StringBuilder url = new StringBuilder().append(pod.getResourceUrl().toString()).append("/log?pretty=false&follow=true&timestamps=true");
        if (sinceTime != null) {
            url.append("&sinceTime=");
        }
        Request request = new Request.Builder().url(new URL(url.toString())).get().build();
        OkHttpClient clone = okHttpClient.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
        clone.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                LOG.info("Failure occurred getting  controller for pod: {},", podName, e);
                handler.accept(null);
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                executor.execute(() -> {
                    try {
                        if (response.code() == 200) {
                            handler.accept(response.body().byteStream());
                        } else {
                            LOG.info("Failure occurred while processing controller for pod: {}, http status: {}, details: {}", podName, response.code(), response.body().string());
                            handler.accept(null);
                        }
                    } catch (IOException e) {
                        LOG.error("Unexpected Error", e);
                    }
                });
            }
        });
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException t) {
        throw new IOException("Unexpected Error", t);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl) URL(java.net.URL)

Example 93 with Callback

use of retrofit2.Callback in project resilience4j by resilience4j.

the class EnqueueDecorator method enqueue.

public static <T> Response<T> enqueue(Call<T> call) throws Throwable {
    final CountDownLatch enqueueLatch = new CountDownLatch(1);
    final AtomicReference<Response<T>> responseReference = new AtomicReference<>();
    final AtomicReference<Throwable> failureReference = new AtomicReference<>();
    call.enqueue(new Callback<T>() {

        @Override
        public void onResponse(final Call<T> call, final Response<T> response) {
            responseReference.set(response);
            enqueueLatch.countDown();
        }

        @Override
        public void onFailure(final Call<T> call, final Throwable t) {
            failureReference.set(t);
            enqueueLatch.countDown();
        }
    });
    enqueueLatch.await();
    if (failureReference.get() != null) {
        throw failureReference.get();
    }
    return responseReference.get();
}
Also used : Response(retrofit2.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 94 with Callback

use of retrofit2.Callback in project edx-app-android by edx.

the class DownloadSpeedService method performDownload.

private synchronized void performDownload(DownloadDescriptor file) {
    final long startTime;
    try {
        startTime = System.nanoTime();
        OkHttpClient client = okHttpClientProvider.getNonOAuthBased().newBuilder().connectTimeout(getResources().getInteger(R.integer.speed_test_timeout_in_milliseconds), TimeUnit.MILLISECONDS).build();
        Request request = new Request.Builder().url(file.getUrl()).build();
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException throwable) {
                logger.error(throwable);
                // If it times out, set a low value for download speed
                setCurrentDownloadSpeed(0.01f);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    logger.debug("Download Speed Test Failed");
                } else {
                    long length = response.body().string().length();
                    double seconds = (System.nanoTime() - startTime) / NS_PER_SEC;
                    if (seconds != 0) {
                        final float downloadSpeedKps = (float) ((length / seconds) / 1024);
                        setCurrentDownloadSpeed(downloadSpeedKps);
                        reportDownloadSpeed(downloadSpeedKps);
                    }
                }
            }
        });
    } catch (Exception ex) {
        logger.error(ex);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException)

Example 95 with Callback

use of retrofit2.Callback in project coolweather by yeliheng.

the class AutoUpdateService method updateBingPic.

/*
    * 更新必应每日一图
    * */
private void updateBingPic() {
    // 必应图片接口
    String requestBingPic = "http://guolin.tech/api/bing_pic";
    HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String bingPic = response.body().string();
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
            // 将从服务器获取到的图片地址存储到SD卡
            editor.putString("bing_pic", bingPic);
            editor.apply();
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) IOException(java.io.IOException)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)151 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Call (retrofit2.Call)90 Retrofit (retrofit2.Retrofit)85 ResponseBody (okhttp3.ResponseBody)64 Test (org.junit.Test)56 GsonBuilder (com.google.gson.GsonBuilder)52 Gson (com.google.gson.Gson)46 OkHttpClient (okhttp3.OkHttpClient)44 Response (retrofit2.Response)43 Callback (retrofit2.Callback)42 RequestBody (okhttp3.RequestBody)39 List (java.util.List)34 File (java.io.File)29 CountDownLatch (java.util.concurrent.CountDownLatch)26 Context (android.content.Context)24 RetrofitAPI (com.gladysinc.gladys.Utils.RetrofitAPI)21