Example usage for com.squareup.okhttp Response code

List of usage examples for com.squareup.okhttp Response code

Introduction

In this page you can find the example usage for com.squareup.okhttp Response code.

Prototype

int code

To view the source code for com.squareup.okhttp Response code.

Click Source Link

Usage

From source file:net.ltgt.resteasy.client.okhttp.OkHttpClientEngine.java

License:Apache License

private ClientResponse createResponse(ClientInvocation request, final Response response) {
    ClientResponse clientResponse = new ClientResponse(request.getClientConfiguration()) {
        private InputStream stream;

        @Override// w w  w . j  av a2  s  .c  o m
        protected InputStream getInputStream() {
            if (stream == null) {
                try {
                    stream = response.body().byteStream();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return stream;
        }

        @Override
        protected void setInputStream(InputStream is) {
            stream = is;
        }

        @Override
        public void releaseConnection() throws IOException {
            // Stream might have been entirely replaced, so we need to close it independently from response.body()
            Throwable primaryExc = null;
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (Throwable t) {
                primaryExc = t;
                throw t;
            } finally {
                if (primaryExc != null) {
                    try {
                        response.body().close();
                    } catch (Throwable suppressedExc) {
                        primaryExc.addSuppressed(suppressedExc);
                    }
                } else {
                    response.body().close();
                }
            }
        }
    };

    clientResponse.setStatus(response.code());
    clientResponse.setHeaders(transformHeaders(response.headers()));

    return clientResponse;
}

From source file:net.protyposis.android.mediaplayer.dash.DashMediaExtractor.java

License:Open Source License

/**
 * Blocking download of a segment./* ww w.j a v  a 2  s .c o m*/
 */
private CachedSegment downloadFile(Integer segmentNr) throws IOException {
    // At the first call, download the initialization segments, and reuse them later.
    if (mInitSegments.isEmpty()) {
        for (Representation representation : mAdaptationSet.representations) {
            Request request = buildSegmentRequest(representation.initSegment);
            long startTime = SystemClock.elapsedRealtime();
            Response response = mHttpClient.newCall(request).execute();
            if (!response.isSuccessful()) {
                throw new IOException("sync dl error @ init segment: " + response.code() + " "
                        + response.message() + " " + request.url().toString());
            }
            ByteString segmentData = response.body().source().readByteString();
            mInitSegments.put(representation, segmentData);
            mAdaptationLogic.reportSegmentDownload(mAdaptationSet, representation,
                    representation.segments.get(segmentNr), segmentData.size(),
                    SystemClock.elapsedRealtime() - startTime);
            Log.d(TAG, "init " + representation.initSegment.toString());
        }
    }

    Segment segment = mRepresentation.segments.get(segmentNr);
    Request request = buildSegmentRequest(segment);
    long startTime = SystemClock.elapsedRealtime();
    Response response = mHttpClient.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new IOException("sync dl error @ segment " + segmentNr + ": " + response.code() + " "
                + response.message() + " " + request.url().toString());
    }
    byte[] segmentData = response.body().bytes();
    mAdaptationLogic.reportSegmentDownload(mAdaptationSet, mRepresentation, segment, segmentData.length,
            SystemClock.elapsedRealtime() - startTime);
    CachedSegment cachedSegment = new CachedSegment(segmentNr, segment, mRepresentation);
    handleSegment(segmentData, cachedSegment);
    Log.d(TAG, "sync dl " + segmentNr + " " + segment.toString() + " -> " + cachedSegment.file.getPath());

    return cachedSegment;
}

From source file:net.qiujuer.common.okhttp.core.HttpCore.java

License:Apache License

/**
 * ============Delivery============/* w  ww .j av a 2 s  .  c  o m*/
 */
protected void deliveryAsyncResult(Request request, HttpCallback<?> callback) {
    Util.log("onDelivery:" + request.url().toString());
    Util.log("Headers:\n" + request.headers().toString());

    final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback;

    //Call start
    callStart(resCallBack, request);

    mOkHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(final Request request, final IOException e) {
            Util.log("onFailure:" + request.toString());
            callFailure(resCallBack, request, null, e);
            callFinish(resCallBack);
        }

        @Override
        public void onResponse(final Response response) {
            try {
                Object ret = null;
                final String string = response.body().string();
                final boolean haveValue = !TextUtils.isEmpty(string);

                Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null"));

                if (haveValue) {
                    ret = mResolver.analysis(string, resCallBack.getClass());
                }

                callSuccess(resCallBack, ret, response.code());
            } catch (Exception e) {
                Util.log("onResponse Failure:" + response.request().toString());
                callFailure(resCallBack, response.request(), response, e);
            }
            callFinish(resCallBack);
        }
    });
}

From source file:net.qiujuer.common.okhttp.core.HttpCore.java

License:Apache License

/**
 * ============Delivery============//from  w  ww .  j  ava2 s. c o m
 */
protected void deliveryAsyncResult(OkHttpClient client, Request request, HttpCallback<?> callback) {
    Util.log("onDelivery:" + request.url().toString());
    Util.log("Headers:\n" + request.headers().toString());

    final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback;

    //Call start
    callStart(resCallBack, request);

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(final Request request, final IOException e) {
            Util.log("onFailure:" + request.toString());
            callFailure(resCallBack, request, null, e);
            callFinish(resCallBack);
        }

        @Override
        public void onResponse(final Response response) {
            try {
                Object ret = null;
                final String string = response.body().string();
                final boolean haveValue = !TextUtils.isEmpty(string);

                Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null"));

                if (haveValue) {
                    ret = mResolver.analysis(string, resCallBack.getClass());
                }

                callSuccess(resCallBack, ret, response.code());
            } catch (Exception e) {
                Util.log("onResponse Failure:" + response.request().toString());
                callFailure(resCallBack, response.request(), response, e);
            }
            callFinish(resCallBack);
        }
    });
}

From source file:net.qiujuer.common.okhttp.core.HttpCore.java

License:Apache License

protected <T> T deliveryResult(Class<T> tClass, Request request, HttpCallback<?> callback) {
    Util.log("onDelivery:" + request.url().toString());
    Util.log("Headers:\n" + request.headers().toString());
    if (callback == null && tClass == null)
        callback = new DefaultCallback();
    final Class<?> subClass = tClass == null ? callback.getClass() : tClass;

    callStart(callback, request);//from   ww  w. ja  v  a 2s  . co  m
    Call call = mOkHttpClient.newCall(request);
    Response response = null;
    Object ret = null;
    try {
        response = call.execute();
        final String string = response.body().string();
        final boolean haveValue = !TextUtils.isEmpty(string);

        Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null"));

        if (haveValue) {
            ret = mResolver.analysis(string, subClass);
        }

        callSuccess(callback, ret, response.code());
    } catch (Exception e) {
        Request req = response == null ? request : response.request();
        Util.log("onResponse Failure:" + req.toString());
        callFailure(callback, req, response, e);
    }
    callFinish(callback);
    return (T) ret;
}

From source file:net.qiujuer.common.okhttp.core.HttpCore.java

License:Apache License

protected void deliveryAsyncResult(final Request request, final StreamCall call,
        final HttpCallback<?> callback) {
    Util.log("onDelivery:" + request.url().toString());
    final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback;

    //Call start//from www  .  j  a v a 2 s.c o m
    callStart(resCallBack, request);

    mOkHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(final Request request, final IOException e) {
            Util.log("onFailure:" + request.toString());
            callFailure(resCallBack, request, null, e);
            callFinish(resCallBack);
        }

        @Override
        public void onResponse(final Response response) {
            OutputStream out = call.getOutputStream();
            InputStream in = null;
            byte[] buf = new byte[mBufferSize];
            try {
                Util.log("onResponse:Code:%d Stream.", response.code());

                ResponseBody body = response.body();
                bindResponseProgressCallback(request.body(), body, callback);

                in = body.byteStream();

                int size;
                while ((size = in.read(buf)) != -1) {
                    out.write(buf, 0, size);
                    out.flush();
                }
                // On success
                call.onSuccess(response.code());
            } catch (Exception e) {
                Util.log("onResponse Failure:" + response.request().toString());
                callFailure(resCallBack, response.request(), response, e);
            } finally {
                com.squareup.okhttp.internal.Util.closeQuietly(in);
                com.squareup.okhttp.internal.Util.closeQuietly(out);
            }
            callFinish(resCallBack);
        }
    });
}

From source file:net.uiqui.oblivion.client.rest.RestOutput.java

License:Apache License

public static RestOutput parse(Response response) throws IOException {
    final int status = response.code();

    final String etagHeader = response.header("ETag");

    Long etag = null;/*from w  w w .  ja  va  2  s  . c  om*/

    if (etagHeader != null) {
        etag = Long.valueOf(etagHeader);
    }

    final String json = response.body().string();

    return new RestOutput(status, etag, json);
}

From source file:net.yatomiya.e4.services.image.UpdateHandler.java

License:Open Source License

@Override
public void onResponse(Response response) throws IOException {
    if (isCanceled) {
        doneUpdate(ImageEvent.createCanceled(entry));
        return;/*from   w ww .  jav a  2 s . c  om*/
    }

    entry.eData.lastNetworkAccessTime = JUtils.getCurrentTime();

    if (response.code() == 200) {
        // OK
        byte[] buf = null;
        buf = response.body().bytes();

        if (buf != null) {

            File file = entry.getStorageFile();
            IOUtils.write(file, buf);
            entry.eData.fileByteSize = file.length();

            try {
                loader = new ImageLoader();
                loader.load(new ByteArrayInputStream(buf));

                if (loader.data == null || loader.data.length <= 0) {
                    throw new SWTException(SWT.ERROR_INVALID_IMAGE,
                            "Loading is successfull but no image in file.");
                }

                entry.eData.lastNetworkUpdateTime = JUtils.getCurrentTime();

                event = ImageEvent.createUpdated(entry, ImageEvent.Source.NETWORK);
            } catch (SWTException e) {
                event = ImageEvent.createSWTExceptionError(entry, ImageEvent.Source.NETWORK, e);
            }
        }
    } else {
        event = ImageEvent.createHttpStatusCodeError(entry, response.code());
    }

    doneUpdate(event);
}

From source file:net.yatomiya.nicherry.services.bbs.BoardUpdateHandler.java

License:Open Source License

void processSettingTxt(Response response) {
    if (response == null)
        return;/*from  ww w.jav  a 2 s .  c o m*/
    if (response.code() != 200)
        return;

    try {
        byte[] content = response.body().bytes();
        if (content.length > 0) {
            File file = getProcessor().getSettingTxtFile();
            IOUtils.write(file, content);
            getProcessor().loadSettingTxt();
        }
    } catch (IOException e) {
    }
}

From source file:net.yatomiya.nicherry.services.bbs.BoardUpdateHandler.java

License:Open Source License

void processHeadTxt(Response response) {
    if (response == null)
        return;//from w ww.j av a2 s. co  m
    if (response.code() != 200)
        return;

    try {
        byte[] content = response.body().bytes();
        if (content.length > 0) {
            File file = getProcessor().getHeadTxtFile();
            IOUtils.write(file, content);
        }
    } catch (IOException e) {
    }
}