gpt4 book ai didi

android - OkHttpClient 与移动运营商问题

转载 作者:行者123 更新时间:2023-12-04 03:57:19 25 4
gpt4 key购买 nike

我的应用程序 API 执行奇怪的行为。我的一个实时应用程序的 API 预计响应时间为 2 到 3 秒,但响应时间很长!

测试用例:

  1. 使用 WIFI =
  2. 与 Airtel 运营商合作 =
  3. 与 Jio 运营商合作 =
  4. 在其他国家工作网络 =
  5. 使用 Vodafone Idea 运营商在移动浏览器中工作 =
  6. 使用 Vodafone Idea 运营商与 IOS 应用程序使用相同的 API =

主要问题:Vodafone Idea carrier 使用 App = Very Slow 时出现问题

案例 1.

 URL url = new URL("https://example.com");
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
int code = urlConnection.getResponseCode();
if (code != 200) {
throw new IOException("Invalid response from server: " + code);
}

BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.e("data", line);
}

结果:

  • 第一次 40 到 50 秒。
  • 在接下来的每个连续调用中,它将按照预期在 2 到 5 秒内返回。

案例 2:

OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

Request.Builder requestBuilder = new Request.Builder()
.url("https://example.com")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
Log.e("APi", "REQUEST: "+request.toString());

Call call1 = okHttpClient.newCall(request);
call1.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.e("APi", "APi Failed");
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.e("APi", "APi isSuccessful:" + response.isSuccessful());
Log.e("APi", "Response:" + response.body().string());
}
});

结果:

  • 每次需要 40 到 50 秒。

案例三:

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS).build();

Request.Builder requestBuilder = new Request.Builder()
.url("https://example.com")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
Log.e("APi", "REQUEST: "+request.toString());

Call call1 = okHttpClient.newCall(request);
call1.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.e("APi", "APi Failed");
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.e("APi", "APi isSuccessful:" + response.isSuccessful());
Log.e("APi", "Response:" + response.body().string());
}
});

结果:

  • 每次需要 5 到 6 秒。

注意:我害怕在应用程序中将 .connectTimeout(1, TimeUnit.SECONDS) 用作 1 秒它开始出现更多 api 故障并且看起来也不是正确的解决方案。

更新案例研究:

https://github.com/square/okhttpOkHttpClient 的详细调试。它给出了大约 4 倍以下的错误。
failed to connect to xxx.com/2001:4860:4802:36::15 (port XXX) from /2402:3a80:1b8f:ca21:847:21a8:5ffc:fc30 (port XXX) after 10000ms  

包 okhttp3.internal.connection.RealConnection 类。尝试建立 connectSocket 但出现错误。

-> 添加“www”后www.example.com进入我的领域。okhttp 库的响应时间从 40 秒变为大约 12 秒。

最佳答案

很难看到全貌,因为你没有解释你是如何执行后续连接的。您完全有可能重用现有连接。默认情况下,HTTP/1.1 使连接保持 Activity 状态。

检查使用纯文本 HTTP 与 HTTPS 是否有区别也可能很有趣,因为 HTTPS 的连接设置成本更高。您的客户端还可以根据返回的证书进行更多验证。

如果更改主机名有如此大的不同,那么我肯定会查看系统上如何配置名称解析器

我还会捕获网络流量 并查看耗时较长的内容。 tcpdump 是你的 friend 或者你总是可以考虑使用 wireshark

正如其他人所建议的,您可能还想禁用盒子上的 IPv6 堆栈以排除问题的根源。

关于android - OkHttpClient 与移动运营商问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com