首页 > 解决方案 > Android - 与 python 脚本相比,改造很慢

问题描述

我在我的 Android 应用程序上使用 Retrofit2,但与通过 Python 脚本进行的相同调用相比,我的调用速度很慢。Android 智能手机和运行脚本的电脑使用相同的互联网连接。

例如,一个 GET 调用(406 字节)平均需要以下时间:

第一次 GET 调用(3140 字节)有最大的不同:

我的改造 API 类代码:

    import java.util.concurrent.TimeUnit;

    import okhttp3.OkHttpClient;
    import okhttp3.logging.HttpLoggingInterceptor;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;

    class APIClient {

        private static Retrofit retrofit = null;
        private static String url = "http://test.mylink.com";

        static Retrofit getClient() {

            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder()
                                    .readTimeout(60, TimeUnit.SECONDS)
                                    .connectTimeout(60, TimeUnit.SECONDS)
                                    .addInterceptor(interceptor).build();

            retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

            return retrofit;
        }

    }

API 调用在线程内进行

class TestClass extends Thread {
     ...
     public void makeCall() {
            Call<Proof> call1 = _apiInterface.getProof();
            call1.enqueue(new Callback<Proof>() {
                @Override
                public void onResponse(Call<Proof> call, Response<Proof> response) {
                    ...
                }

                @Override
                public void onFailure(Call<Proof> call, Throwable t) {
                    call.cancel();
                    ...
                }
            });
     }
}

有没有人在改造时遇到过类似的问题?

标签: androidperformanceretrofitretrofit2okhttp

解决方案


推荐阅读