如果无法连接到服务器,如何更改url

qvk1mo1f  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(369)

在我的doinbackgroundonasynctask中,我有一个代码来检查try-and-catch中的url,如果与服务器的连接中断,我想将url更改为备份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改成这样:

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

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

o75abkj4

o75abkj41#

来自android文档
设置连接超时

public void setConnectTimeout (int timeout)

设置打开指向此urlconnection引用的资源的通信链接时要使用的指定超时值(以毫秒为单位)。如果超时在建立连接之前过期,则会引发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";
}

相关问题