Example usage for com.squareup.okhttp Call enqueue

List of usage examples for com.squareup.okhttp Call enqueue

Introduction

In this page you can find the example usage for com.squareup.okhttp Call enqueue.

Prototype

public void enqueue(Callback responseCallback) 

Source Link

Document

Schedules the request to be executed at some point in the future.

Usage

From source file:me.lock8.Mzigo.java

License:Apache License

public void enqueue(final Request request, final Callback callback) {

    if (checkForDuplicatedRequest(request))
        return;/*  w  w w.j  a  va 2 s  .  com*/

    final Call call = client.newCall(request);
    journalCall(request, call, callback);

    call.enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            notifyFailure(request, e);
            unJournalCall(request, callback);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            notifyResponse(response);
            unJournalCall(request, callback);

        }
    });
}

From source file:microsoft.aspnet.signalr.client.http.android.AndroidOkHttpConnection.java

License:Open Source License

@Override
public HttpConnectionFuture execute(final Request request, final ResponseCallback responseCallback) {

    mLogger.log("Create new AsyncTask for HTTP Connection", LogLevel.Verbose);

    final HttpConnectionFuture future = new HttpConnectionFuture();

    com.squareup.okhttp.Request okHttpRequest = createRequest(request);

    final Call call = client.newCall(okHttpRequest);
    call.enqueue(new Callback() {
        @Override/*from   w w  w  . ja  va  2s  .  c o m*/
        public void onFailure(com.squareup.okhttp.Request request, IOException e) {
            mLogger.log("Error executing request: " + e.getMessage(), LogLevel.Critical);
            future.triggerError(e);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            mLogger.log("Request executed", LogLevel.Verbose);

            InputStream bodyStream = response.body().byteStream();
            Map<String, List<String>> headersMap = response.headers().toMultimap();
            try {
                responseCallback.onResponse(new StreamResponse(bodyStream, response.code(), headersMap));
                future.setResult(null);
            } catch (Exception e) {
                mLogger.log("Error calling onResponse: " + e.getMessage(), LogLevel.Critical);
                future.triggerError(e);
            }
        }
    });

    future.onCancelled(new Runnable() {
        @Override
        public void run() {
            call.cancel();
        }
    });

    return future;
}

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

License:Open Source License

/**
 * Makes async segment requests to fill the cache up to a certain level.
 *//*from   w w w .  j a  v a  2 s .c  o m*/
private synchronized void fillFutureCache(Representation representation) {
    int segmentsToBuffer = (int) Math.ceil((double) mMinBufferTimeUs / mRepresentation.segmentDurationUs);
    for (int i = mCurrentSegment + 1; i < Math.min(mCurrentSegment + 1 + segmentsToBuffer,
            mRepresentation.segments.size()); i++) {
        if (!mFutureCache.containsKey(i) && !mFutureCacheRequests.containsKey(i)) {
            Segment segment = representation.segments.get(i);
            Request request = buildSegmentRequest(segment);
            Call call = mHttpClient.newCall(request);
            CachedSegment cachedSegment = new CachedSegment(i, segment, representation); // segment could be accessed through representation by i
            call.enqueue(new SegmentDownloadCallback(cachedSegment));
            mFutureCacheRequests.put(i, call);
        }
    }
}

From source file:net.yatomiya.e4.util.StandardHttpClient.java

License:Open Source License

public Call execute(Request request, Callback callback, boolean isSynchronous) {
    Call call = newCall(request);
    if (isSynchronous) {
        try {// ww w . j a va2  s  .  co  m
            Response response = call.execute();
            callback.onResponse(response);
        } catch (IOException e) {
            callback.onFailure(request, e);
        }
    } else {
        call.enqueue(callback);
    }
    return call;
}

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

License:Open Source License

@Override
public Call execute(Request request, Callback callback, boolean isSynchronous) {
    Call call = newCall(request);
    if (isSynchronous) {
        try {/*w  ww .j  av  a  2s .c  o  m*/
            Response response = call.execute();
            callback.onResponse(response);
        } catch (IOException e) {
            callback.onFailure(request, e);
        }
    } else {
        call.enqueue(callback);
    }
    return call;
}

From source file:pct.droid.base.providers.subs.SubsProvider.java

License:Open Source License

/**
 * @param context      Context/*  ww  w .j a  v a 2s  . co m*/
 * @param media        Media data
 * @param languageCode Code of language
 * @param callback     Network callback
 * @return Call
 */
public static Call download(final Context context, final Media media, final String languageCode,
        final com.squareup.okhttp.Callback callback) {
    OkHttpClient client = PopcornApplication.getHttpClient();
    if (media.subtitles != null && media.subtitles.containsKey(languageCode)) {
        try {
            Request request = new Request.Builder().url(media.subtitles.get(languageCode)).build();
            Call call = client.newCall(request);

            final File subsDirectory = getStorageLocation(context);
            final String fileName = media.videoId + "-" + languageCode;
            final File srtPath = new File(subsDirectory, fileName + ".srt");

            if (srtPath.exists()) {
                callback.onResponse(null);
                return call;
            }

            call.enqueue(new com.squareup.okhttp.Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    callback.onFailure(request, e);
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    if (response.isSuccessful()) {
                        InputStream inputStream = null;
                        boolean failure = false;
                        try {

                            subsDirectory.mkdirs();
                            if (srtPath.exists()) {
                                File to = new File(subsDirectory, "temp" + System.currentTimeMillis());
                                srtPath.renameTo(to);
                                to.delete();
                            }

                            inputStream = response.body().byteStream();
                            String urlString = response.request().urlString();

                            if (urlString.contains(".zip") || urlString.contains(".gz")) {
                                SubsProvider.unpack(inputStream, srtPath, languageCode);
                            } else if (SubsProvider.isSubFormat(urlString)) {
                                parseFormatAndSave(urlString, srtPath, languageCode, inputStream);
                            } else {
                                callback.onFailure(response.request(),
                                        new IOException("FatalParsingException"));
                                failure = true;
                            }
                        } catch (FatalParsingException e) {
                            e.printStackTrace();
                            callback.onFailure(response.request(), new IOException("FatalParsingException"));
                            failure = true;
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            callback.onFailure(response.request(), e);
                            failure = true;
                        } catch (IOException e) {
                            e.printStackTrace();
                            callback.onFailure(response.request(), e);
                            failure = true;
                        } finally {
                            if (inputStream != null)
                                inputStream.close();

                            if (!failure)
                                callback.onResponse(response);
                        }
                    } else {
                        callback.onFailure(response.request(), new IOException("Unknown error"));
                    }
                }
            });

            return call;
        } catch (RuntimeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    callback.onFailure(null, new IOException("Wrong media"));
    return null;
}

From source file:retrofit.KOkHttpCall.java

License:Apache License

private void exeRequest(final Callback<T> callback, final Request request, final boolean loadnetElseCache) {
    com.squareup.okhttp.Call rawCall;
    try {//from   w ww . ja  v  a2  s.co  m
        rawCall = client.newCall(request);
    } catch (Throwable t) {
        callback.onFailure(t);
        return;
    }
    if (canceled) {
        rawCall.cancel();
    }
    this.rawCall = rawCall;
    rawCall.enqueue(new com.squareup.okhttp.Callback() {
        private void callFailure(Throwable e) {
            try {
                callback.onFailure(e);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        private void callSuccess(Response<T> response) {
            try {
                callback.onResponse(response);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        @Override
        public void onFailure(Request request, IOException e) {
            callFailure(e);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response rawResponse) {
            Response<T> response;
            try {
                response = parseResponse(rawResponse, request);
            } catch (Throwable e) {
                if (loadnetElseCache) {
                    cacheExecutor.execute(new CallbackRunnable<T>(callback, cacheCallbackExecutor) {
                        @Override
                        public Response<T> obtainResponse() {
                            return execCacheRequest(request);
                        }
                    });
                    return;
                }
                callFailure(e);
                return;
            }
            callSuccess(response);
        }
    });
}

From source file:retrofit.OkHttpCall.java

License:Apache License

@Override
public void enqueue(final Callback<T> callback) {
    synchronized (this) {
        if (executed)
            throw new IllegalStateException("Already executed");
        executed = true;/*w w  w.j  a v a  2 s . c  o m*/
    }

    com.squareup.okhttp.Call rawCall;
    try {
        rawCall = createRawCall();
    } catch (Throwable t) {
        callback.onFailure(t);
        return;
    }
    if (canceled) {
        rawCall.cancel();
    }
    this.rawCall = rawCall;

    rawCall.enqueue(new com.squareup.okhttp.Callback() {
        private void callFailure(Throwable e) {
            try {
                callback.onFailure(e);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        private void callSuccess(Response<T> response) {
            try {
                callback.onResponse(response, retrofit);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        @Override
        public void onFailure(Request request, IOException e) {
            callFailure(e);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response rawResponse) {
            Response<T> response;
            try {
                response = parseResponse(rawResponse);
            } catch (Throwable e) {
                callFailure(e);
                return;
            }
            callSuccess(response);
        }
    });
}

From source file:retrofit2.OkHttpCall.java

License:Apache License

@Override
public void enqueue(final Callback<T> callback) {
    synchronized (this) {
        if (executed)
            throw new IllegalStateException("Already executed.");
        executed = true;/*from  w  w  w  .  ja  va 2 s .  co  m*/
    }

    com.squareup.okhttp.Call rawCall;
    try {
        rawCall = createRawCall();
    } catch (Throwable t) {
        callback.onFailure(t);
        return;
    }
    if (canceled) {
        rawCall.cancel();
    }
    this.rawCall = rawCall;

    rawCall.enqueue(new com.squareup.okhttp.Callback() {
        private void callFailure(Throwable e) {
            try {
                callback.onFailure(e);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        private void callSuccess(Response<T> response) {
            try {
                callback.onResponse(response);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        @Override
        public void onFailure(Request request, IOException e) {
            callFailure(e);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response rawResponse) {
            Response<T> response;
            try {
                response = parseResponse(rawResponse);
            } catch (Throwable e) {
                callFailure(e);
                return;
            }
            callSuccess(response);
        }
    });
}