Search in sources :

Example 61 with Call

use of zipkin2.Call in project portal by ixinportal.

the class OkHttpClientManagerTime method _downloadAsyn.

/**
 * 同步的Post请求
 *
 * @param url
 * @param params post的参数
 * @return 字符串
 */
// private String _postAsString(String url, Param... params) throws IOException
// {
// Response response = _post(url, params);
// return response.body().string();
// }
/**
 * 异步的post请求
 *
 * @param url
 * @param callback
 * @param params
 */
// private void _postAsyn(String url, Callback callback, Param... params)
// {
// Request request = buildPostRequest(url, params);
// deliveryResult(callback, request);
// }
/**
 * 异步的post请求
 *
 * @param url
 * @param callback
 * @param params
 */
// private void _postAsyn(String url, Callback callback, Map<String, String> params)
// {
// Request request = buildPostRequest(url, params);
// deliveryResult(callback, request);
// }
// private void _postAsyn(String url, Callback callback, String params)
// {
// Request request = buildPostRequest(url, params);
// deliveryResult(callback, request);
// }
// /**
// * 同步基于post的文件上传
// *
// * @param params
// * @return
// */
// private Response _post(String url, File[] files, String[] fileKeys, Param... params) throws IOException
// {
// Request request = buildMultipartFormRequest(url, files, fileKeys, params);
// return mOkHttpClient.newCall(request).execute();
// }
// private Response _post(String url, File file, String fileKey) throws IOException
// {
// Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
// return mOkHttpClient.newCall(request).execute();
// }
// private Response _post(String url, File file, String fileKey, Param... params) throws IOException
// {
// Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
// return mOkHttpClient.newCall(request).execute();
// }
// /**
// * 异步基于post的文件上传
// *
// * @param url
// * @param callback
// * @param files
// * @param fileKeys
// * @throws IOException
// */
// private void _postAsyn(String url, Callback callback, File[] files, String[] fileKeys, Param... params) throws IOException
// {
// Request request = buildMultipartFormRequest(url, files, fileKeys, params);
// deliveryResult(callback, request);
// }
// /**
// * 异步基于post的文件上传,单文件不带参数上传
// *
// * @param url
// * @param callback
// * @param file
// * @param fileKey
// * @throws IOException
// */
// private void _postAsyn(String url, Callback callback, File file, String fileKey) throws IOException
// {
// Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
// deliveryResult(callback, request);
// }
// /**
// * 异步基于post的文件上传,单文件且携带其他form参数上传
// *
// * @param url
// * @param callback
// * @param file
// * @param fileKey
// * @param params
// * @throws IOException
// */
// private void _postAsyn(String url, Callback callback, File file, String fileKey, Param... params) throws IOException
// {
// Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
// deliveryResult(callback, request);
// }
/**
 * 异步下载文件
 *
 * @param url
 * @param destFileDir 本地文件存储的文件夹
 * @param callback
 */
private void _downloadAsyn(final String url, final String destFileDir, final Callback callback) {
    final Request request = new Request.Builder().url(url).build();
    final Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            sendFailedStringCallback(call, e, callback);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            InputStream is = null;
            byte[] buf = new byte[2048];
            int len = 0;
            FileOutputStream fos = null;
            try {
                is = response.body().byteStream();
                File file = new File(destFileDir, getFileName(url));
                fos = new FileOutputStream(file);
                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                fos.flush();
                // 如果下载文件成功,第一个参数为文件的绝对路径
                sendSuccessResultCallback(file.getAbsolutePath(), callback);
            } catch (IOException e) {
                sendFailedStringCallback(call, e, callback);
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                }
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                }
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File)

Example 62 with Call

use of zipkin2.Call in project portal by ixinportal.

the class OkHttpClientManagerTime method _getAsResponse.

/**
 * 同步的Get请求
 *
 * @param url
 * @return Response
 */
private Response _getAsResponse(String url) throws IOException {
    final Request request = new Request.Builder().url(url).build();
    Call call = mOkHttpClient.newCall(request);
    Response execute = call.execute();
    return execute;
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Request(okhttp3.Request)

Example 63 with Call

use of zipkin2.Call in project ocreader by schaal.

the class API method login.

public static void login(final Context context, final HttpUrl baseUrl, final String username, final String password, final APICallback<Status, LoginError> loginCallback) {
    final HttpManager httpManager = new HttpManager(username, password, baseUrl);
    final HttpUrl resolvedBaseUrl = baseUrl.resolve("");
    if (resolvedBaseUrl == null) {
        loginCallback.onFailure(new LoginError("Couldn't parse URL"));
        return;
    }
    final Moshi moshi = new Moshi.Builder().build();
    final JsonAdapter<NewsError> errorJsonAdapter = moshi.adapter(NewsError.class);
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(resolvedBaseUrl).client(httpManager.getClient()).addConverterFactory(MoshiConverterFactory.create(moshi)).build();
    final CommonAPI commonAPI = retrofit.create(CommonAPI.class);
    commonAPI.apiLevels().enqueue(new Callback<APILevels>() {

        @Override
        public void onResponse(@NonNull Call<APILevels> call, @NonNull Response<APILevels> response) {
            if (response.isSuccessful()) {
                loginInstance = null;
                final APILevels apiLevels = response.body();
                final Level apiLevel = apiLevels != null ? apiLevels.highestSupportedApi() : null;
                loginInstance = Level.getAPI(context, apiLevel);
                if (apiLevel == null) {
                    loginCallback.onFailure(new LoginError(context.getString(R.string.error_not_compatible)));
                } else {
                    loginInstance.setupApi(httpManager);
                    loginInstance.metaData(new Callback<Status>() {

                        @Override
                        public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
                            if (response.isSuccessful()) {
                                final Status status = response.body();
                                final Version version = status != null ? status.getVersion() : null;
                                if (version != null && MIN_VERSION.lessThanOrEqualTo(version)) {
                                    PreferenceManager.getDefaultSharedPreferences(context).edit().putString(Preferences.USERNAME.getKey(), username).putString(Preferences.PASSWORD.getKey(), password).putString(Preferences.URL.getKey(), resolvedBaseUrl.toString()).putString(Preferences.SYS_DETECTED_API_LEVEL.getKey(), apiLevel.getLevel()).apply();
                                    instance = loginInstance;
                                    loginInstance = null;
                                    loginCallback.onSuccess(status);
                                } else {
                                    if (version != null) {
                                        Log.d(TAG, String.format("Nextcloud News version is less than minimally supported version: %s < %s", version.toString(), MIN_VERSION.toString()));
                                        loginCallback.onFailure(new LoginError(context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
                                    } else {
                                        Log.d(TAG, "Couldn't parse Nextcloud News version");
                                        loginCallback.onFailure(new LoginError(context.getString(R.string.failed_detect_nc_version)));
                                    }
                                }
                            } else {
                                String message = getErrorMessage(errorJsonAdapter, response);
                                Log.d(TAG, "Metadata call failed with error: " + message);
                                loginCallback.onFailure(LoginError.getError(context, response.code(), message));
                            }
                        }

                        @Override
                        public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
                            Log.e(TAG, "Failed to log in", t);
                            loginCallback.onFailure(LoginError.getError(context, t));
                        }
                    });
                }
            } else {
                Log.d(TAG, "API level call failed with error: " + response.code() + " " + getErrorMessage(errorJsonAdapter, response));
                // Either nextcloud news is not installed or version prior 8.8.0
                loginCallback.onFailure(LoginError.getError(context, response.code(), context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
            }
        }

        @Override
        public void onFailure(@NonNull Call<APILevels> call, @NonNull Throwable t) {
            Log.e(TAG, "Failed to log in", t);
            loginCallback.onFailure(LoginError.getError(context, t));
        }
    });
}
Also used : NewsError(email.schaal.ocreader.api.json.NewsError) Status(email.schaal.ocreader.api.json.Status) Call(retrofit2.Call) Moshi(com.squareup.moshi.Moshi) HttpManager(email.schaal.ocreader.http.HttpManager) LoginError(email.schaal.ocreader.util.LoginError) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) APILevels(email.schaal.ocreader.api.json.APILevels) Callback(retrofit2.Callback) Version(com.github.zafarkhaja.semver.Version) NonNull(android.support.annotation.NonNull)

Example 64 with Call

use of zipkin2.Call in project KalturaGeneratedAPIClientsJava by kaltura.

the class APIOkRequestsExecutor method cancelRequest.

@Override
public void cancelRequest(String reqId) {
    Dispatcher dispatcher = getOkClient(null).dispatcher();
    Call call = findCall(reqId, dispatcher.queuedCalls());
    if (call != null) {
        call.cancel();
    }
    call = findCall(reqId, dispatcher.runningCalls());
    if (call != null) {
        call.cancel();
    }
}
Also used : Call(okhttp3.Call) Dispatcher(okhttp3.Dispatcher)

Example 65 with Call

use of zipkin2.Call in project KalturaGeneratedAPIClientsJava by kaltura.

the class APIOkRequestsExecutor method hasRequest.

// TODO: cancel check on executor + null check on provider
// @Override
public boolean hasRequest(String reqId) {
    Dispatcher dispatcher = getOkClient(null).dispatcher();
    Call call = findCall(reqId, dispatcher.queuedCalls());
    if (call != null) {
        return true;
    }
    call = findCall(reqId, dispatcher.runningCalls());
    return call != null;
}
Also used : Call(okhttp3.Call) Dispatcher(okhttp3.Dispatcher)

Aggregations

Call (okhttp3.Call)409 Response (okhttp3.Response)309 Request (okhttp3.Request)282 IOException (java.io.IOException)232 Call (retrofit2.Call)134 Callback (okhttp3.Callback)133 OkHttpClient (okhttp3.OkHttpClient)98 Test (org.junit.Test)88 ResponseBody (okhttp3.ResponseBody)76 RequestBody (okhttp3.RequestBody)58 Retrofit (retrofit2.Retrofit)48 Gson (com.google.gson.Gson)47 Response (retrofit2.Response)47 File (java.io.File)44 Headers (okhttp3.Headers)41 Callback (retrofit2.Callback)41 GsonBuilder (com.google.gson.GsonBuilder)40 JSONObject (org.json.JSONObject)39 MockResponse (okhttp3.mockwebserver.MockResponse)38 List (java.util.List)35