Search in sources :

Example 66 with ResponseBody

use of okhttp3.ResponseBody in project okhttp by square.

the class OkHttpContributors method main.

public static void main(String... args) throws Exception {
    OkHttpClient client = new OkHttpClient();
    // Create request for remote resource.
    Request request = new Request.Builder().url(ENDPOINT).build();
    // Execute the request and retrieve the response.
    Response response = client.newCall(request).execute();
    // Deserialize HTTP response to concrete type.
    ResponseBody body = response.body();
    List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
    body.close();
    // Sort list by the most contributions.
    Collections.sort(contributors, new Comparator<Contributor>() {

        @Override
        public int compare(Contributor c1, Contributor c2) {
            return c2.contributions - c1.contributions;
        }
    });
    // Output list of contributors.
    for (Contributor contributor : contributors) {
        System.out.println(contributor.login + ": " + contributor.contributions);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody)

Example 67 with ResponseBody

use of okhttp3.ResponseBody in project picasso by square.

the class NetworkRequestHandlerTest method unknownContentLengthFromDiskThrows.

@Test
public void unknownContentLengthFromDiskThrows() throws Exception {
    final AtomicBoolean closed = new AtomicBoolean();
    ResponseBody body = new ResponseBody() {

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 0;
        }

        @Override
        public BufferedSource source() {
            return new Buffer();
        }

        @Override
        public void close() {
            closed.set(true);
            super.close();
        }
    };
    responses.add(responseOf(body).newBuilder().cacheResponse(responseOf(null)).build());
    Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
    try {
        networkHandler.load(action.getRequest(), 0);
        fail();
    } catch (IOException expected) {
        verifyZeroInteractions(stats);
        assertTrue(closed.get());
    }
}
Also used : Buffer(okio.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 68 with ResponseBody

use of okhttp3.ResponseBody in project glide by bumptech.

the class OkHttpStreamFetcher method loadData.

@Override
public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();
    call = client.newCall(request);
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp failed to obtain result", e);
            }
            callback.onLoadFailed(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            responseBody = response.body();
            if (response.isSuccessful()) {
                long contentLength = responseBody.contentLength();
                stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
            } else if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message());
            }
            callback.onDataReady(stream);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Request(okhttp3.Request) IOException(java.io.IOException) Map(java.util.Map)

Example 69 with ResponseBody

use of okhttp3.ResponseBody in project amhttp by Eddieyuan123.

the class OnAddListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnAddListener.this.getClass());
        T bean = null;
        if (type != null) {
            if (TextUtils.equals(type.toString(), "class java.lang.String"))
                bean = (T) responseStr;
            else
                bean = new Gson().fromJson(responseStr, type);
        }
        final T finalBean = bean;
        Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess(finalBean);
            }
        });
    }
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Example 70 with ResponseBody

use of okhttp3.ResponseBody in project amhttp by Eddieyuan123.

the class OnUploadListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnUploadListener.this.getClass());
        T bean = null;
        if (type != null) {
            if (TextUtils.equals(type.toString(), "class java.lang.String"))
                bean = (T) responseStr;
            else
                bean = new Gson().fromJson(responseStr, type);
        }
        final T finalBean = bean;
        Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess(finalBean);
            }
        });
    }
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Aggregations

ResponseBody (okhttp3.ResponseBody)584 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)332 DateTime (org.joda.time.DateTime)332 ServiceCall (com.microsoft.rest.ServiceCall)140 Test (org.junit.Test)123 Request (okhttp3.Request)112 Observable (rx.Observable)97 Response (retrofit2.Response)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 List (java.util.List)64 IOException (java.io.IOException)33 Response (okhttp3.Response)33 Buffer (okio.Buffer)33 RequestBody (okhttp3.RequestBody)31 PageImpl (com.microsoft.azure.batch.protocol.models.PageImpl)26 MockResponse (okhttp3.mockwebserver.MockResponse)24 InputStream (java.io.InputStream)19 MultipartBody (okhttp3.MultipartBody)16