Android网络时间更新失败报错SntpClient: request time failed: java.net.SocketTimeoutException: Poll timed out

本文详细介绍了在Android设备上遇到的SNTP时间同步失败问题,主要原因是无法连接到Google的时间服务器。通过修改config_ntpServer配置,将时间服务器更改为苹果的time1.apple.com,成功解决了这一问题。

找到报错的地方:
frameworks\base\core\ java\android\net\SntpClient.java

/**
 * Sends an SNTP request to the given host and processes the response.
 *
 * @param host host name of the server.
 * @param timeout network timeout in milliseconds.
 * @param network network over which to send the request.
 * @return true if the transaction was successful.
 */
public boolean requestTime(String host, int timeout, Network network) {
    final Network networkForResolv = network.getPrivateDnsBypassingCopy();
    InetAddress address = null;
    try {
        address = networkForResolv.getByName(host);
    } catch (Exception e) {
        EventLogTags.writeNtpFailure(host, e.toString());
        if (DBG) Log.d(TAG, "request time failed: " + e);
        return false;
    }
    return requestTime(address, NTP_PORT, timeout, networkForResolv);
}

发现时间服务器出错。

修改:
\frameworks\base\core\res\res\values\config.xml

<!-- <string translatable="false" name="config_ntpServer">time.android.com</string> -->
<string translatable="false" name="config_ntpServer">time1.apple.com</string>

总结国内连接不上谷歌的SNTP服务器,time.android.com,time.google.com都连接不上。
安卓是1-4,
time1.google.com
time2.google.com
time3.google.com
time4.google.com

修改config_ntpServer为苹果的时间服务器,time1.apple.com,苹果服务器是1-7.

SNTP协议客户端实现以及数据包格式: LI:跳跃指示器,警告在当月最后一天的最终时刻插入的迫近闺秒(闺秒)。 VN:版本号。 Mode:工作模式。该字段包括以下值:0-预留;1-对称行为;3-客户机;4-服务器;5-广播;6-NTP控制信息。NTP协议具有3种工作模式,分别为主/被动对称模式、客户/服务器模式、广播模式。在主/被动对称模式中,有一对一的连接,双方均可同步对方或被对方同步,先发出申请建立连接的一方工作在主动模式下,另一方工作在被动模式下; 客户/服务器模 式与主/被动模式基本相同,惟一区别在于客户方可被服务器同步,但服务器不能被客户同步;在广播模式中,有一对多的连接,服务器不论客户工作 在何种模式下,都会主动发出时间信息,客户根据此信息调整自己的时间。 Stratum:对本地时钟级别的整体识别。 Poll:有符号整数表示连续信息间的最大间隔。 Precision:有符号整数表示本地时钟精确度。 Root Delay:表示到达主参考源的一次往复的总延迟,它是有15~16位小数部分的符号定点小 数。 Root Dispersion:表示一次到达主参考源的标准误差,它是有15~16位小数部分的无符号 定点小数。 Reference Identifier:识别特殊参考源。 Originate Timestamp:这是向服务器请求分离客户机的时间,采用64位时标格式。 Receive Timestamp:这是向服务器请求到达客户机的时间,采用64位时标格式。 Transmit Timestamp:这是向客户机答复分离服务器的时间,采用64位时标格式。 Authenticator(Optional):当实现了NTP认证模式时,主要标识符和信息数字域就 包括已定义的信息认证代码(MAC)信息。
当在准备Android SDK包时出现`java.net.SocketTimeoutException: Read timed out`错误,通常是由于网络连接超时导致的。可以从以下几个方面解决: ### 增加超时时间 在开发过程中,可以尝试增加网络请求的超时时间。在代码中如果使用`HttpURLConnection`或`OkHttp`等网络请求库,可以通过设置相应的超时参数来实现。 使用`HttpURLConnection`的示例代码如下: ```java import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class HttpExample { public static void main(String[] args) { try { URL url = new URL("your_url"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置连接超时时间为5000毫秒(5秒) connection.setConnectTimeout(5000); // 设置读取超时时间为5000毫秒(5秒) connection.setReadTimeout(5000); connection.connect(); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用`OkHttp`的示例代码如下: ```java import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; import java.util.concurrent.TimeUnit; public class OkHttpExample { public static void main(String[] args) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS) .build(); Request request = new Request.Builder() .url("your_url") .build(); try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { System.out.println(response.body().string()); } } catch (IOException e) { e.printStackTrace(); } } } ``` ### 检查网络环境 确保网络连接稳定,可以尝试切换网络环境,如从Wi-Fi切换到移动数据,或者反之。同时,检查防火墙或代理设置,确保没有阻止网络请求。 ### 检查服务器端设置 参考在解决`java.net.SocketTimeoutException: Read timed out`错误时考虑对方服务超时时间的思路,检查SDK服务器端的超时时间设置是否过短。如果是,可以联系服务器管理员进行调整。 ### 重试机制 在代码中实现重试机制,当出现超时错误时,自动进行重试。示例代码如下: ```java import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class RetryExample { private static final int MAX_RETRIES = 3; public static void main(String[] args) { int retries = 0; while (retries < MAX_RETRIES) { try { URL url = new URL("your_url"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); break; } catch (IOException e) { retries++; if (retries == MAX_RETRIES) { e.printStackTrace(); } } } } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值