Search in sources :

Example 11 with Call

use of zipkin2.Call in project Rocket.Chat.Android by RocketChat.

the class DefaultServerPolicyApi method getOkHttpCallback.

private okhttp3.Callback getOkHttpCallback(@NonNull FlowableEmitter<Response<JSONObject>> emitter, @NonNull String protocol) {
    return new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException ioException) {
            if (emitter.isCancelled()) {
                return;
            }
            emitter.onError(ioException);
        }

        @Override
        public void onResponse(Call call, okhttp3.Response response) throws IOException {
            if (emitter.isCancelled()) {
                return;
            }
            if (!response.isSuccessful()) {
                emitter.onNext(new Response<>(false, protocol, null));
                emitter.onComplete();
                return;
            }
            final ResponseBody body = response.body();
            if (body == null || body.contentLength() == 0) {
                emitter.onNext(new Response<>(false, protocol, null));
                emitter.onComplete();
                return;
            }
            try {
                emitter.onNext(new Response<>(true, protocol, new JSONObject(body.string())));
            } catch (Exception e) {
                emitter.onNext(new Response<>(false, protocol, null));
            }
            emitter.onComplete();
        }
    };
}
Also used : Call(okhttp3.Call) JSONObject(org.json.JSONObject) IOException(java.io.IOException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 12 with Call

use of zipkin2.Call in project Gradle-demo by Arisono.

the class OkhttpUtils method sendGetHttp.

/** 
	 * get http 
	 * @param url
	 * @param tag
	 */
public static void sendGetHttp(String url, Map<String, Object> params, String cookies, String tag) {
    StringBuilder buf = new StringBuilder(url);
    if (params != null) {
        if (!params.isEmpty()) {
            if (url.indexOf("?") == -1)
                buf.append("?");
            else if (!url.endsWith("&"))
                buf.append("&");
            Iterator<Map.Entry<String, Object>> entries = params.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, Object> entry = entries.next();
                buf.append(String.valueOf(entry.getKey())).append("=").append(String.valueOf(entry.getValue())).append("&");
            }
            buf.deleteCharAt(buf.length() - 1);
        }
    }
    Request request = new Request.Builder().url(buf.toString()).addHeader("content-type", "text/html;charset:utf-8").addHeader("Cookie", "12").build();
    OkhttpUtils.println(tag + ":" + buf.toString());
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String requestJson;
            requestJson = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(requestJson);
            RxBus.getInstance().send(tag + ":" + requestJson);
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(call, e, this);
        }
    });
}
Also used : Call(okhttp3.Call) Builder(okhttp3.FormBody.Builder) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) Map(java.util.Map)

Example 13 with Call

use of zipkin2.Call in project realm-java by realm.

the class OkHttpAuthenticationServer method logout.

private LogoutResponse logout(URL logoutUrl, String requestBody) throws Exception {
    Request request = new Request.Builder().url(logoutUrl).addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").post(RequestBody.create(JSON, requestBody)).build();
    Call call = client.newCall(request);
    Response response = call.execute();
    return LogoutResponse.from(response);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Request(okhttp3.Request)

Example 14 with Call

use of zipkin2.Call in project okhttp by square.

the class RealWebSocket method connect.

public void connect(OkHttpClient client) {
    client = client.newBuilder().protocols(ONLY_HTTP1).build();
    final int pingIntervalMillis = client.pingIntervalMillis();
    final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
    call = Internal.instance.newWebSocketCall(client, request);
    call.enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) {
            try {
                checkResponse(response);
            } catch (ProtocolException e) {
                failWebSocket(e, response);
                closeQuietly(response);
                return;
            }
            // Promote the HTTP streams into web socket streams.
            StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
            // Prevent connection pooling!
            streamAllocation.noNewStreams();
            Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
            // Process all web socket messages.
            try {
                listener.onOpen(RealWebSocket.this, response);
                String name = "OkHttp WebSocket " + request.url().redact();
                initReaderAndWriter(name, pingIntervalMillis, streams);
                streamAllocation.connection().socket().setSoTimeout(0);
                loopReader();
            } catch (Exception e) {
                failWebSocket(e, null);
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            failWebSocket(e, null);
        }
    });
}
Also used : Response(okhttp3.Response) StreamAllocation(okhttp3.internal.connection.StreamAllocation) Call(okhttp3.Call) ProtocolException(java.net.ProtocolException) Callback(okhttp3.Callback) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 15 with Call

use of zipkin2.Call in project retrofit by square.

the class CallsTest method deferredThrowExecute.

@Test
public void deferredThrowExecute() throws IOException {
    final IOException failure = new IOException("Hey");
    Call<Object> failing = Calls.defer(new Callable<Call<Object>>() {

        @Override
        public Call<Object> call() throws Exception {
            throw failure;
        }
    });
    try {
        failing.execute();
        fail();
    } catch (IOException e) {
        assertSame(failure, e);
    }
}
Also used : Call(retrofit2.Call) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Call (okhttp3.Call)409 Response (okhttp3.Response)309 Request (okhttp3.Request)282 IOException (java.io.IOException)232 Call (retrofit2.Call)134 Callback (okhttp3.Callback)133 OkHttpClient (okhttp3.OkHttpClient)98 Test (org.junit.Test)88 ResponseBody (okhttp3.ResponseBody)76 RequestBody (okhttp3.RequestBody)58 Retrofit (retrofit2.Retrofit)48 Gson (com.google.gson.Gson)47 Response (retrofit2.Response)47 File (java.io.File)44 Headers (okhttp3.Headers)41 Callback (retrofit2.Callback)41 GsonBuilder (com.google.gson.GsonBuilder)40 JSONObject (org.json.JSONObject)39 MockResponse (okhttp3.mockwebserver.MockResponse)38 List (java.util.List)35