Java Code Examples for com.squareup.okhttp.Call#execute()

The following examples show how to use com.squareup.okhttp.Call#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiClient.java    From ariADDna with Apache License 2.0 6 votes vote down vote up
/**
 * Execute HTTP call and deserialize the HTTP response body into the given return type.
 *
 * @param returnType The return type used to deserialize HTTP response body
 * @param <T> The return type corresponding to (same with) returnType
 * @param call Call
 * @return ApiResponse object containing response status, headers and
 *   data, which is a Java object deserialized from response body and would be null
 *   when returnType is null.
 * @throws ApiException If fail to execute the call
 */
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {

    try {
        Response response = call.execute();
        LOGGER.info("Method {execute} was called, with params isExecuted: {}, returnType: {}",
                call.isExecuted(), returnType == null ? null : returnType.getTypeName());
        T data = handleResponse(response, returnType);
        return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
    } catch (IOException e) {
        LOGGER.error("Method {execute} was called, with params :{},{}", call.isExecuted(),
                returnType == null ? null : returnType.getTypeName());
        LOGGER.error("Method {execute} throw exception: " + e);
        throw new ApiException(e);
    }
}
 
Example 2
Source File: OkHttpDownloadRequest.java    From meiShi with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T invoke(Class<T> clazz) throws IOException
{
    final Call call = mOkHttpClient.newCall(request);
    Response response = call.execute();
    return (T) saveFile(response, null);
}
 
Example 3
Source File: Ok2Lite.java    From httplite with Apache License 2.0 4 votes vote down vote up
@Override
public Response execute(Request request) throws Exception {
    Call call = mClient.newCall(makeRequest(request));
    request.handle().setHandle(new CallHandle(call));
    return new OkResponse(call.execute(),request);
}
 
Example 4
Source File: RPC.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
private static String postRpc(Method method, Object[] args) {
    String responseStr = null;
    boolean retry = true;
    int executionCount = 0;
    while (retry) {
        try {
            if (executionCount > 0) {
                Thread.sleep(RETRY_SLEEP_TIME_MILLIS * executionCount);
            }
            executionCount++;
            Request.Builder builder = new Request.Builder().url(Config.HTTP_START + "/androidRpc?method=" + method.getName());
            if (args != null) {
                // String encode = ToolStream.encode(args);
                // LOG.debug("encoded string : " + encode.length());
                // LOG.debug("gzipped string : " + ToolStream.encodeGzip(args).length());
                Object[] args_logs = new Object[3];
                args_logs[0] = args;
                builder.post(RequestBody.create(MediaType.parse("*/*"), Streams.encodeGzip(args_logs)));
            } else {
                builder.post(RequestBody.create(MediaType.parse("*/*"), ""));
            }

            LOG.info("postRpc " + method.getName() + "(" + Joiner.on(',').useForNull("null").join(args) + ")");

            Request request = builder.build();
            Call call = client.newCall(request);
            Response response = call.execute();

            responseStr = response.body().string();
            retry = false;
        } catch (Throwable e) {
            LOG.error("Failed androidRpc (" + e.getClass().getCanonicalName() + ")  executionCount=" + executionCount + " : " + method.getName() + " : " + Arrays.toString(args));
            if (e instanceof InterruptedIOException || e instanceof SSLHandshakeException) {
                retry = false;
            } else if (executionCount >= DEFAULT_MAX_RETRIES) {
                retry = false;
            }
            if (e instanceof UnknownHostException || e instanceof SocketException) {
                notifyNetworkError();
            }
        }
    }
    return responseStr;
}
 
Example 5
Source File: ApiClient.java    From nifi-api-client-java with Apache License 2.0 3 votes vote down vote up
/**
 * Execute HTTP call and deserialize the HTTP response body into the given return type.
 *
 * @param returnType The return type used to deserialize HTTP response body
 * @param <T> The return type corresponding to (same with) returnType
 * @param call Call
 * @return ApiResponse object containing response status, headers and
 *   data, which is a Java object deserialized from response body and would be null
 *   when returnType is null.
 * @throws ApiException If fail to execute the call
 */
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
    try {
        Response response = call.execute();
        T data = handleResponse(response, returnType);
        return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
    } catch (IOException e) {
        throw new ApiException(e);
    }
}