Search in sources :

Example 21 with okhttp3

use of okhttp3 in project Retrofit-Android-Basic by basil2style.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, final String authToken) {
    if (authToken != null) {
        httpClient.interceptors().add(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();
                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder().header("Authorization", authToken).method(original.method(), original.body());
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }
    OkHttpClient client = httpClient.build();
    Retrofit retrofit = builder.client(client).build();
    return retrofit.create(serviceClass);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor)

Example 22 with okhttp3

use of okhttp3 in project xmall by Exrick.

the class QiniuUtil method qiniuBase64Upload.

public static String qiniuBase64Upload(String data64) {
    String key = renamePic(".png");
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    // 服务端http://up-z2.qiniup.com
    String url = "http://up-z2.qiniup.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(key);
    RequestBody rb = RequestBody.create(null, data64);
    Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/octet-stream").addHeader("Authorization", "UpToken " + getUpToken()).post(rb).build();
    System.out.println(request.headers());
    OkHttpClient client = new OkHttpClient();
    okhttp3.Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(response);
    return origin + key;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Auth(com.qiniu.util.Auth) Request(okhttp3.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 23 with okhttp3

use of okhttp3 in project curb by irijwj.

the class RetrofitGetData method postAnswer.

public static String postAnswer(String strEntity) {
    RequestBody description = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
    Call<String> postAnswerCall = serverInterface.postAnswer(description);
    Response<String> response;
    String postResult = null;
    try {
        response = postAnswerCall.execute();
        if (response.isSuccessful()) {
            postResult = response.body();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return postResult;
}
Also used : IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 24 with okhttp3

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

the class RunIotaAPICore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    boolean USE_AUTH = false;
    // if(host.contains(".runplay.com") || host.contains(".runpg.com"))
    // USE_AUTH=true;
    final String nodeUrl = protocol + "://" + host + ":" + port;
    if (USE_AUTH) {
        // Log.e("IRI-CONNECT","Using Auth OK");
        String creds = Base64.encodeToString((uname + ":" + upassword).getBytes(), false);
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        OkHttpClient client = builder.readTimeout(5000, TimeUnit.SECONDS).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).addHeader("Authorization", "Basic " + creds).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);
    } else {
        // Log.e("IRI-CONNECT","NOOTTTTT Using Auth");
        final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).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);
    }
    // Create OkHttpBuilder
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor)

Example 25 with okhttp3

use of okhttp3 in project okhttp-OkGo by jeasonlzy.

the class BaseRequest method getCall.

/** 获取同步call对象 */
public okhttp3.Call getCall() {
    //构建请求体,返回call对象
    RequestBody requestBody = generateRequestBody();
    mRequest = generateRequest(wrapRequestBody(requestBody));
    return generateCall(mRequest);
}
Also used : RequestBody(okhttp3.RequestBody)

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