android - Android - 与 python 脚本相比,改造很慢
问题描述
我在我的 Android 应用程序上使用 Retrofit2,但与通过 Python 脚本进行的相同调用相比,我的调用速度很慢。Android 智能手机和运行脚本的电脑使用相同的互联网连接。
例如,一个 GET 调用(406 字节)平均需要以下时间:
- 安卓应用:525ms
- Python 脚本:111 毫秒
- 野生动物园:109 毫秒
第一次 GET 调用(3140 字节)有最大的不同:
- 安卓应用:1723ms
- Python 脚本或 Safari:294 毫秒
我的改造 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();
...
}
});
}
}
有没有人在改造时遇到过类似的问题?
解决方案
推荐阅读
- c++ - 带/不带指针的函数原型使用区别
- python - conda activate 和 conda.bat activate 不激活 Powershell 中的虚拟环境
- javascript - php 不适用于 html 中的所有 $_POST
- flutter - 为什么我从负数的模运算中得到不正确的输出
- javascript - 需要帮助使用 discord bot 命令分配变量
- javascript - 使用哈希表或哈希映射将数组中的对象值更新为其他数组中的值
- html - 响应式 Web 开发问题 - 重叠元素
- python-3.x - 我将如何使用 x 和 y 坐标在我的 pygame 游戏中移动一块
- c# - 如何使用互操作(c#.Net)迭代excel(.xlsm)工作表行和列
- asp.net-web-api - CRM web api与过滤器一起扩展-内连接还是左连接?