我究竟做错了什么?我的超时无效

问题描述 投票:1回答:1

我已将Wi-Fi IP更改为无法连接。我希望当5秒钟传球做其他事情但是等待大约20秒。

URL url = null;
HttpsURLConnection conn = null;

try {
    url = new URL("MY_URL");
    conn = (HttpsURLConnection) url.openConnection();
    conn.setReadTimeout(3000);
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("msg", String.valueOf(jsonArray)));

    OutputStream os = conn.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
    writer.write(getQuery(params));
    writer.flush();
    writer.close();
    conn.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) conn.getContent(), "UTF-8"));

    String response = reader.readLine();
android post timeout httpsurlconnection
1个回答
0
投票

来自documentation

警告:如果主机名解析为多个IP地址,则Android的默认HttpURLConnection实现将按RFC 3484顺序尝试。如果连接到这些地址中的每一个都失败,则在连接尝试引发异常之前将经过多次超时。支持IPv6和IPv4的主机名始终至少有2个IP地址。

这意味着,如果主机涉及“n”个IP地址,则需要n*milliseconds时间而不是milliseconds

© www.soinside.com 2019 - 2024. All rights reserved.