Search in sources :

Example 91 with OkHttpClient

use of okhttp3.OkHttpClient in project cw-omnibus by commonsguy.

the class OkHttpTests method syncTest.

@Test
public void syncTest() throws IOException {
    OkHttpClient client = new OkHttpClient.Builder().build();
    Request request = new Request.Builder().url(URL).build();
    Response response = client.newCall(request).execute();
    Assert.assertEquals(EXPECTED, response.body().string());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 92 with OkHttpClient

use of okhttp3.OkHttpClient in project cw-omnibus by commonsguy.

the class QuestionsFragment method onViewCreated.

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    OkHttpClient client = ((MainActivity) getActivity()).getOkHttpClient();
    Request request = new Request.Builder().url(SO_URL).build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(getClass().getSimpleName(), "Exception loading JSON", e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                Reader in = response.body().charStream();
                BufferedReader reader = new BufferedReader(in);
                final SOQuestions questions = new Gson().fromJson(reader, SOQuestions.class);
                reader.close();
                view.post(new Runnable() {

                    @Override
                    public void run() {
                        setListAdapter(new ItemsAdapter(questions.items));
                    }
                });
            } else {
                Log.e(getClass().getSimpleName(), response.toString());
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) BufferedReader(java.io.BufferedReader)

Example 93 with OkHttpClient

use of okhttp3.OkHttpClient in project Meizhi by drakeet.

the class GankFragment method getOldVideoPreview.

private void getOldVideoPreview(OkHttpClient client) {
    String url = "http://gank.io/" + String.format("%s/%s/%s", mYear, mMonth, mDay);
    Request request = new Request.Builder().url(url).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 {
            String body = response.body().string();
            mVideoPreviewUrl = LoveStrings.getVideoPreviewImageUrl(body);
            startPreview(mVideoPreviewUrl);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException)

Example 94 with OkHttpClient

use of okhttp3.OkHttpClient in project buck by facebook.

the class DistBuildFactory method newFrontendService.

public static FrontendService newFrontendService(CommandRunnerParams params) {
    DistBuildConfig config = new DistBuildConfig(params.getBuckConfig());
    ClientSideSlb slb = config.getFrontendConfig().createClientSideSlb(params.getClock(), params.getBuckEventBus(), new CommandThreadFactory("StampedeNetworkThreadPool", SLB_THREAD_PRIORITY));
    OkHttpClient client = config.createOkHttpClient();
    return new FrontendService(ThriftOverHttpServiceConfig.of(new LoadBalancedService(slb, client, params.getBuckEventBus())));
}
Also used : ClientSideSlb(com.facebook.buck.slb.ClientSideSlb) OkHttpClient(okhttp3.OkHttpClient) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) FrontendService(com.facebook.buck.distributed.FrontendService) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) DistBuildConfig(com.facebook.buck.distributed.DistBuildConfig)

Example 95 with OkHttpClient

use of okhttp3.OkHttpClient in project buck by facebook.

the class DefaultDefectReporter method uploadReport.

private DefectSubmitResult uploadReport(final DefectReport defectReport, DefectSubmitResult.Builder defectSubmitResult, ClientSideSlb slb) throws IOException {
    long timeout = rageConfig.getHttpTimeout();
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).build();
    HttpService httpService = new RetryingHttpService(buckEventBus, new LoadBalancedService(slb, httpClient, buckEventBus), rageConfig.getMaxUploadRetries());
    try {
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.addHeader(REQUEST_PROTOCOL_VERSION, rageConfig.getProtocolVersion().name().toLowerCase());
        requestBuilder.post(new RequestBody() {

            @Override
            public MediaType contentType() {
                return MediaType.parse("application/x-www-form-urlencoded");
            }

            @Override
            public void writeTo(BufferedSink bufferedSink) throws IOException {
                writeReport(defectReport, bufferedSink.outputStream());
            }
        });
        HttpResponse response = httpService.makeRequest(rageConfig.getReportUploadPath(), requestBuilder);
        String responseBody;
        try (InputStream inputStream = response.getBody()) {
            responseBody = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
        }
        if (response.statusCode() == HTTP_SUCCESS_CODE) {
            defectSubmitResult.setIsRequestSuccessful(true);
            if (rageConfig.getProtocolVersion().equals(AbstractRageConfig.RageProtocolVersion.SIMPLE)) {
                return defectSubmitResult.setReportSubmitMessage(responseBody).setReportSubmitLocation(responseBody).build();
            } else {
                // Decode Json response.
                RageJsonResponse json = objectMapper.readValue(responseBody.getBytes(Charsets.UTF_8), RageJsonResponse.class);
                return defectSubmitResult.setIsRequestSuccessful(json.getRequestSuccessful()).setReportSubmitErrorMessage(json.getErrorMessage()).setReportSubmitMessage(json.getMessage()).setReportSubmitLocation(json.getRageUrl()).build();
            }
        } else {
            throw new IOException(String.format("Connection to %s returned code %d and message: %s", response.requestUrl(), response.statusCode(), responseBody));
        }
    } catch (IOException e) {
        throw new IOException(String.format("Failed uploading report because [%s].", e.getMessage()));
    } finally {
        httpService.close();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) MediaType(okhttp3.MediaType) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) RequestBody(okhttp3.RequestBody)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)149 Request (okhttp3.Request)73 Response (okhttp3.Response)61 IOException (java.io.IOException)52 Test (org.junit.Test)35 Call (okhttp3.Call)24 Retrofit (retrofit2.Retrofit)24 Interceptor (okhttp3.Interceptor)19 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)18 MockResponse (okhttp3.mockwebserver.MockResponse)18 File (java.io.File)15 GsonBuilder (com.google.gson.GsonBuilder)12 Provides (dagger.Provides)12 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 Gson (com.google.gson.Gson)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Cache (okhttp3.Cache)10 Observable (rx.Observable)10 Singleton (javax.inject.Singleton)9