Search in sources :

Example 66 with Callback

use of zipkin2.Callback in project okhttp by square.

the class AsynchronousGet method run.

public void run() throws Exception {
    Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
    client.newCall(request).enqueue(new Callback() {

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

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try (ResponseBody responseBody = response.body()) {
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);
                Headers responseHeaders = response.headers();
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                System.out.println(responseBody.string());
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 67 with Callback

use of zipkin2.Callback in project materialistic by hidroh.

the class FileDownloader method downloadFile.

@WorkerThread
public void downloadFile(String url, String mimeType, FileDownloaderCallback callback) {
    File outputFile = new File(mCacheDir, new File(url).getName());
    if (outputFile.exists()) {
        mMainHandler.post(() -> callback.onSuccess(outputFile.getPath()));
        return;
    }
    final Request request = new Request.Builder().url(url).addHeader("Content-Type", mimeType).build();
    mCallFactory.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            mMainHandler.post(() -> callback.onFailure(call, e));
        }

        @Override
        public void onResponse(Call call, Response response) {
            try {
                BufferedSink sink = Okio.buffer(Okio.sink(outputFile));
                sink.writeAll(response.body().source());
                sink.close();
                mMainHandler.post(() -> callback.onSuccess(outputFile.getPath()));
            } catch (IOException e) {
                this.onFailure(call, e);
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) File(java.io.File) WorkerThread(androidx.annotation.WorkerThread)

Example 68 with Callback

use of zipkin2.Callback in project pinpoint by naver.

the class OkHttpClient_3_0_0_to_3_3_x_IT method enqueue.

@Test
public void enqueue() throws Exception {
    Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build();
    OkHttpClient client = new OkHttpClient();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Response> responseRef = new AtomicReference<>(null);
    client.newCall(request).enqueue(new Callback() {

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

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            responseRef.set(response);
            latch.countDown();
        }
    });
    latch.await(3, TimeUnit.SECONDS);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.awaitTrace(event(ASYNC.getName(), "Asynchronous Invocation"), 20, 3000);
    verifier.printCache();
    Method realCallEnqueueMethod = Class.forName("okhttp3.RealCall").getDeclaredMethod("enqueue", Class.forName("okhttp3.Callback"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), realCallEnqueueMethod));
    Method dispatcherEnqueueMethod = Class.forName("okhttp3.Dispatcher").getDeclaredMethod("enqueue", Class.forName("okhttp3.RealCall$AsyncCall"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), dispatcherEnqueueMethod));
    verifier.verifyTrace(event(ASYNC.getName(), "Asynchronous Invocation"));
    Method executeMethod = Class.forName("okhttp3.RealCall$AsyncCall").getDeclaredMethod("execute");
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), executeMethod));
    Method sendRequestMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("sendRequest");
    verifier.verifyTrace(event(OK_HTTP_CLIENT.getName(), sendRequestMethod, null, null, webServer.getHostAndPort(), annotation("http.url", request.url().toString())));
    String hostAndPort = HostAndPort.toHostAndPortString(request.url().host(), request.url().port());
    Method connectMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("connect");
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), connectMethod, annotation("http.internal.display", hostAndPort)));
    Response response = responseRef.get();
    Method readResponseMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("readResponse");
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), readResponseMethod, annotation("http.status.code", response.code())));
    verifier.verifyTraceCount(0);
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Response(okhttp3.Response) Callback(okhttp3.Callback) Test(org.junit.Test)

Example 69 with Callback

use of zipkin2.Callback in project pinpoint by naver.

the class OkHttpClient_3_4_0_BaseIT method enqueue.

@Test
public void enqueue() throws Exception {
    Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build();
    OkHttpClient client = new OkHttpClient();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Response> responseRef = new AtomicReference<>(null);
    client.newCall(request).enqueue(new Callback() {

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

        @Override
        public void onResponse(Call call, Response response) {
            responseRef.set(response);
            latch.countDown();
        }
    });
    latch.await(3, TimeUnit.SECONDS);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.awaitTrace(event(ASYNC.getName(), "Asynchronous Invocation"), 20, 3000);
    verifier.printCache();
    Method realCallEnqueueMethod = Class.forName("okhttp3.RealCall").getDeclaredMethod("enqueue", Class.forName("okhttp3.Callback"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), realCallEnqueueMethod));
    Method dispatcherEnqueueMethod = Class.forName("okhttp3.Dispatcher").getDeclaredMethod("enqueue", Class.forName("okhttp3.RealCall$AsyncCall"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), dispatcherEnqueueMethod));
    verifier.verifyTrace(event(ASYNC.getName(), "Asynchronous Invocation"));
    Method executeMethod = Class.forName("okhttp3.RealCall$AsyncCall").getDeclaredMethod("execute");
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), executeMethod));
    Response response = responseRef.get();
    Method interceptMethod = Class.forName("okhttp3.internal.http.BridgeInterceptor").getDeclaredMethod("intercept", Class.forName("okhttp3.Interceptor$Chain"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT.getName(), interceptMethod, null, null, webServer.getHostAndPort(), annotation("http.url", request.url().toString()), annotation("http.status.code", response.code())));
    String hostAndPort = HostAndPort.toHostAndPortString(request.url().host(), request.url().port());
    Method connectMethod = getConnectMethod(Class.forName("okhttp3.internal.connection.RealConnection"));
    verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), connectMethod, annotation("http.internal.display", hostAndPort)));
    verifier.verifyTraceCount(0);
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Response(okhttp3.Response) Callback(okhttp3.Callback) Test(org.junit.Test)

Example 70 with Callback

use of zipkin2.Callback in project yyl_example by Relucent.

the class OkhttpTest2 method main.

public static void main(String[] args) throws IOException {
    OkHttpClient client;
    (client = // 
    new OkHttpClient.Builder().build()).newCall(// 
    new Request.Builder().url(// 
    "https://www.baidu.com/").header("Connection", // close | keep-alive
    "close").get().build()).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            System.out.println(response.body().string());
        }

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }
    });
    // 应用关闭时候需要关闭线程池
    client.dispatcher().executorService().shutdown();
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) IOException(java.io.IOException)

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