Search in sources :

Example 46 with okhttp3

use of okhttp3 in project Palm300Heroes by nicolite.

the class HttpUtils method sendOkHttpRequest.

/**
 * 采用OkHttp请求
 *
 * @param address  地址
 * @param callback 回调
 */
public static void sendOkHttpRequest(final String address, okhttp3.Callback callback) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(address).build();
    client.newCall(request).enqueue(callback);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request)

Example 47 with okhttp3

use of okhttp3 in project run-wallet-android by runplay.

the class IotaMsgCore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    final String nodeUrl = protocol + "://" + host + ":" + port;
    // Create OkHttpBuilder
    Authenticator auth = new Authenticator() {

        @Nullable
        @Override
        public Request authenticate(Route route, okhttp3.Response response) throws IOException {
            if (responseCount(response) >= 3) {
                // If we've failed 3 times, give up. - in real life, never give up!!
                return null;
            }
            String credential = Credentials.basic(uname, upassword);
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    };
    final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).authenticator(auth).addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Request newRequest;
            newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
            return chain.proceed(newRequest);
        }
    }).connectTimeout(5000, TimeUnit.SECONDS).build();
    // use client to create Retrofit service
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
    service = retrofit.create(IotaAPIService.class);
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : IotaAPIService(jota.IotaAPIService) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Aggregations

Request (okhttp3.Request)25 IOException (java.io.IOException)20 Test (org.junit.Test)16 ResponseBody (okhttp3.ResponseBody)12 OkHttpClient (okhttp3.OkHttpClient)10 Response (okhttp3.Response)9 RequestBody (okhttp3.RequestBody)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 Call (okhttp3.Call)5 Interceptor (okhttp3.Interceptor)5 Map (java.util.Map)4 Retrofit (retrofit2.Retrofit)4 Header (retrofit2.http.Header)4 List (java.util.List)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Response (retrofit2.Response)3 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)3 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Level (java.util.logging.Level)2