Search in sources :

Example 46 with Callback

use of zipkin2.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 47 with Callback

use of zipkin2.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)

Example 48 with Callback

use of zipkin2.Callback in project coolweather by yeliheng.

the class AutoUpdateService method updateWeather.

/*
    * 更新天气信息
    * */
private void updateWeather() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String weatherString = prefs.getString("weather", null);
    if (weatherString != null) {
        Weather weather = Utility.handleWeatherResponse(weatherString);
        String weatherId = weather.basic.weatherId;
        // 请求地址
        String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId + "&key=" + key;
        HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {

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

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                Weather weather = Utility.handleWeatherResponse(responseText);
                if (weather != null && "ok".equals(weather.status)) {
                    // 判断信息是否有效
                    // 存储数据到SD卡
                    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                    editor.putString("weather", responseText);
                    editor.apply();
                }
            }
        });
    }
}
Also used : Weather(com.coolweather.android.gson.Weather) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException)

Example 49 with Callback

use of zipkin2.Callback in project okHttp-Util-gson by xwdz.

the class HttpManager method execute.

public void execute(final Request request, final CallBack callBack, final Class c) {
    Call call = mClient.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            if (callBack != null) {
                callBack.error(call, e);
            }
        }

        @Override
        public void onResponse(final Call call, final Response response) {
            if (callBack != null) {
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        if (!response.isSuccessful()) {
                            IOException ioException = new IOException("request failed , reponse's code is : " + response.code());
                            callBack.error(call, ioException);
                            return;
                        }
                        if (call.isCanceled()) {
                            callBack.error(call, new IOException("Canceled!"));
                            return;
                        }
                        try {
                            final String json = response.body().string();
                            final Object object = Parser.getInstance().parser(json, c);
                            callBack.onNativeResponse(call, response);
                            callBack.response(call, object);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    });
    mCalls.add(call);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) IOException(java.io.IOException)

Example 50 with Callback

use of zipkin2.Callback in project mapbox-events-android by mapbox.

the class TelemetryClientMapEventsTest method sendsTheCorrectBodyPostingMultipleEvents.

@Test
public void sendsTheCorrectBodyPostingMultipleEvents() throws Exception {
    TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "anyUserAgent");
    Event.Type load = Event.Type.MAP_LOAD;
    Event loadEvent = obtainMapEvent(load);
    Event.Type click = Event.Type.MAP_CLICK;
    Event clickEvent = obtainMapEvent(click);
    Callback mockedCallback = mock(Callback.class);
    enqueueMockResponse();
    List<Event> events = obtainEvents(loadEvent, clickEvent);
    telemetryClient.sendEvents(events, mockedCallback);
    String expectedRequestBody = obtainExpectedRequestBody(new GsonBuilder(), events.get(0), events.get(1));
    assertRequestBodyEquals(expectedRequestBody);
}
Also used : Callback(okhttp3.Callback) GsonBuilder(com.google.gson.GsonBuilder) Test(org.junit.Test)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14