Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果url未连接到服务器,如何更改url_Java_Android_Android Asynctask - Fatal编程技术网

Java 如果url未连接到服务器,如何更改url

Java 如果url未连接到服务器,如何更改url,java,android,android-asynctask,Java,Android,Android Asynctask,在我的doInBackground on AsyncTask中,我有一个代码来检查try and catch中的URL,如果与服务器的连接中断,我想将URL更改为备份URL 这是我的密码: protected String doInBackground(String... params) { try { // Enter URL address where your php file resides ur

在我的doInBackground on AsyncTask中,我有一个代码来检查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");
我怎样才能做到这一点?提前感谢

来自

设置连接超时

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";
}

您已经在try块中添加了超时,如果超时发生,它将被捕获到catch块中,请为该特定异常添加另一个catch块并更改url,然后重新连接。