Example usage for com.squareup.okhttp Request url

List of usage examples for com.squareup.okhttp Request url

Introduction

In this page you can find the example usage for com.squareup.okhttp Request url.

Prototype

HttpUrl url

To view the source code for com.squareup.okhttp Request url.

Click Source Link

Usage

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

License:Open Source License

/**
 * Blocking download of a segment.// w  ww .j  ava 2s.  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 a va  2  s  .  com
 */
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.ja va2s . c  om
 */
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);/*  w  ww  .  j a v a 2s.c o 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.qiujuer.common.okhttp.DefaultCallback.java

License:Apache License

@Override
public void onFailure(Request request, Response response, Exception e) {
    Util.log("onFailed:" + (request != null ? request.url() : "url is null"));
}

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

License:Apache License

public Response intercept(final Chain chain) throws IOException {
    final Request request = chain.request();

    try {// w  w  w  .  jav a  2 s .  co  m
        return chain.proceed(request);
    } catch (SocketTimeoutException e) {
        cluster.nextServer();

        final Server server = cluster.server();
        final URL url = URLBuilder.change(request.url(), server.getServer(), server.getPort());
        final Request retry = request.newBuilder().url(url).build();

        return chain.proceed(retry);
    }
}

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

License:Open Source License

public void waitInterval(Request request) {
    String host = request.url().getHost();
    HostData data;//from   www  . ja va  2 s.  c  om
    synchronized (this) {
        data = dataMap.get(host);
        if (data == null) {
            data = new HostData();
            data.host = host;

            dataMap.put(host, data);
        }
    }
    synchronized (data) {
        long current = System.currentTimeMillis();
        if (data.lastAccessTime >= 0) {
            long elapsed = current - data.lastAccessTime;
            long remain = interval - elapsed;
            if (remain > 0) {
                try {
                    Thread.sleep(remain);
                } catch (InterruptedException e) {
                }
            }
            data.lastAccessTime = current;
        }
    }
}

From source file:org.addhen.birudo.data.net.LoggingInterceptor.java

License:Apache License

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Timber.i(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(),
            request.headers()));//from w  w w  .j  av a  2s .  c o m

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Timber.i(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d,
            response.headers()));

    return response;
}

From source file:org.apache.nifi.processors.standard.InvokeHTTP.java

License:Apache License

private void logRequest(ComponentLog logger, Request request) {
    logger.debug("\nRequest to remote service:\n\t{}\n{}",
            new Object[] { request.url().toExternalForm(), getLogString(request.headers().toMultimap()) });
}