【问题标题】:how to change url if it doesnt connect to server如果没有连接到服务器如何更改url
【发布时间】:2021-03-26 16:34:57
【问题描述】:

在 AsyncTask 上的 doInBackground 中,我有一个代码来检查 URL 中的 try and catch,如果与服务器的连接断开,我想将 URL 更改为备份的。

这是我的代码:


protected String doInBackground(String... params) {
            try {

                // Enter URL address where your php file resides
                url = new URL("http://"+IPADDR+"/"+NMSERVER+"/loginNIKUUID.inc.php");

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "exception";
            }
            try {
                // Setup HttpURLConnection class to send and receive data from php and mysql
                conn = (HttpURLConnection)url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("POST");

                // setDoInput and setDoOutput method depict handling of both send and receive
                conn.setDoInput(true);
                conn.setDoOutput(true);

                // Append parameters to URL
                Uri.Builder builder;
                builder = new Uri.Builder();
                //builder.appendQueryParameter("imei", params[0]);
                builder.appendQueryParameter("nik", params[0]);
                builder.appendQueryParameter("uuid", params[1]);
                builder.appendQueryParameter("password", params[2]);
                String query = builder.build().getEncodedQuery();

                // Open connection for sending data
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, StandardCharsets.UTF_8));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();
                conn.connect();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return "exception";
            }

我想把网址改成这样:

 url = new URL("http://"+IPADDR2+"/"+NMSERVER2+"/loginNIKUUID.inc.php");

我怎样才能做到这一点?提前致谢

【问题讨论】:

  • 您已经在 try 块中添加了 Timeout,如果发生超时,它将被捕获到 catch 块中,为该特定异常添加另一个 catch 块并更改 url 并再次进行连接。

标签: java android android-asynctask


【解决方案1】:

来自Android Documentation

setConnectTimeout

public void setConnectTimeout (int timeout)

设置一个指定的超时值,以毫秒为单位,当 打开到此引用的资源的通信链接 网址连接。如果在连接之前超时过期 建立后,会引发 java.net.SocketTimeoutException。超时 零被解释为无限超时。

解决方案

当客户端在超时时间内无法连接到服务器时,应用会抛出一个SocketTimeoutException,所以我们可以使用这个行为来切换到备用服务器。

String mainUrl = "http://" + IPADDR + "/" + NMSERVER + "/loginNIKUUID.inc.php";
String backupUrl = "http://" + IPADDR2 + "/" + NMSERVER2 + "/loginNIKUUID.inc.php";

@Override
protected String doInBackground(String... params) {
    return connect(mainUrl, params);
}

private String connect(String hostUrl, String...params) {
    try {
        // Enter URL address where your php file resides
        url = new URL(hostUrl);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return "exception";
    }

    try {
        // Setup HttpURLConnection class to send and receive data from php and mysql
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(READ_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod("POST");

        // setDoInput and setDoOutput method depict handling of both send and receive
        conn.setDoInput(true);
        conn.setDoOutput(true);

        // Append parameters to URL
        Uri.Builder builder;
        builder = new Uri.Builder();
        //builder.appendQueryParameter("imei", params[0]);
        builder.appendQueryParameter("nik", params[0]);
        builder.appendQueryParameter("uuid", params[1]);
        builder.appendQueryParameter("password", params[2]);
        String query = builder.build().getEncodedQuery();

        // Open connection for sending data
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, StandardCharsets.UTF_8));
        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        conn.connect();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();

        // There is a problem with the main server, switch to backup server
        if (hostUrl.equals(mainUrl)) {
            return connect(backupUrl, params);
        }

        return "exception";
    } catch (IOException e) {
        e.printStackTrace();
        return "exception";
    }

    return "success";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2020-07-07
    相关资源
    最近更新 更多