Search in sources :

Example 11 with Callback

use of zipkin2.Callback in project okhttp by square.

the class OkHttpAsync method prepare.

@Override
public void prepare(final Benchmark benchmark) {
    concurrencyLevel = benchmark.concurrencyLevel;
    targetBacklog = benchmark.targetBacklog;
    client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
    if (benchmark.tls) {
        SslClient sslClient = SslClient.localhost();
        SSLSocketFactory socketFactory = sslClient.socketFactory;
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession session) {
                return true;
            }
        };
        client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    }
    callback = new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("Failed: " + e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            ResponseBody body = response.body();
            long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
            long finish = System.nanoTime();
            if (VERBOSE) {
                long start = (Long) response.request().tag();
                System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
            }
            requestsInFlight.decrementAndGet();
        }
    };
}
Also used : Call(okhttp3.Call) SslClient(okhttp3.internal.tls.SslClient) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) HostnameVerifier(javax.net.ssl.HostnameVerifier) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 12 with Callback

use of zipkin2.Callback in project portal by ixinportal.

the class OkHttpClientManagerPdfVerify 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 13 with Callback

use of zipkin2.Callback in project portal by ixinportal.

the class OkHttpClientManagerSave 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 14 with Callback

use of zipkin2.Callback 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 15 with Callback

use of zipkin2.Callback 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)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14