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

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

在我的doinbackgroundonasynctask中,我有一个代码来检查try-and-catch中的url,如果与服务器的连接中断,我想将url更改为备份url。
这是我的密码:

  1. protected String doInBackground(String... params) {
  2. try {
  3. // Enter URL address where your php file resides
  4. url = new URL("http://"+IPADDR+"/"+NMSERVER+"/loginNIKUUID.inc.php");
  5. } catch (MalformedURLException e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. return "exception";
  9. }
  10. try {
  11. // Setup HttpURLConnection class to send and receive data from php and mysql
  12. conn = (HttpURLConnection)url.openConnection();
  13. conn.setReadTimeout(READ_TIMEOUT);
  14. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  15. conn.setRequestMethod("POST");
  16. // setDoInput and setDoOutput method depict handling of both send and receive
  17. conn.setDoInput(true);
  18. conn.setDoOutput(true);
  19. // Append parameters to URL
  20. Uri.Builder builder;
  21. builder = new Uri.Builder();
  22. //builder.appendQueryParameter("imei", params[0]);
  23. builder.appendQueryParameter("nik", params[0]);
  24. builder.appendQueryParameter("uuid", params[1]);
  25. builder.appendQueryParameter("password", params[2]);
  26. String query = builder.build().getEncodedQuery();
  27. // Open connection for sending data
  28. OutputStream os = conn.getOutputStream();
  29. BufferedWriter writer = new BufferedWriter(
  30. new OutputStreamWriter(os, StandardCharsets.UTF_8));
  31. writer.write(query);
  32. writer.flush();
  33. writer.close();
  34. os.close();
  35. conn.connect();
  36. } catch (IOException e1) {
  37. // TODO Auto-generated catch block
  38. e1.printStackTrace();
  39. return "exception";
  40. }

我想把url改成这样:

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

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

o75abkj4

o75abkj41#

来自android文档
设置连接超时

  1. public void setConnectTimeout (int timeout)

设置打开指向此urlconnection引用的资源的通信链接时要使用的指定超时值(以毫秒为单位)。如果超时在建立连接之前过期,则会引发java.net.sockettimeoutexception。超时为零被解释为无限超时。
解决方案
当客户端无法在超时时间内连接到服务器时,应用程序将抛出 SocketTimeoutException ,因此我们可以使用此行为切换到备份服务器。

  1. String mainUrl = "http://" + IPADDR + "/" + NMSERVER + "/loginNIKUUID.inc.php";
  2. String backupUrl = "http://" + IPADDR2 + "/" + NMSERVER2 + "/loginNIKUUID.inc.php";
  3. @Override
  4. protected String doInBackground(String... params) {
  5. return connect(mainUrl, params);
  6. }
  7. private String connect(String hostUrl, String...params) {
  8. try {
  9. // Enter URL address where your php file resides
  10. url = new URL(hostUrl);
  11. } catch (MalformedURLException e) {
  12. e.printStackTrace();
  13. return "exception";
  14. }
  15. try {
  16. // Setup HttpURLConnection class to send and receive data from php and mysql
  17. conn = (HttpURLConnection) url.openConnection();
  18. conn.setReadTimeout(READ_TIMEOUT);
  19. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  20. conn.setRequestMethod("POST");
  21. // setDoInput and setDoOutput method depict handling of both send and receive
  22. conn.setDoInput(true);
  23. conn.setDoOutput(true);
  24. // Append parameters to URL
  25. Uri.Builder builder;
  26. builder = new Uri.Builder();
  27. //builder.appendQueryParameter("imei", params[0]);
  28. builder.appendQueryParameter("nik", params[0]);
  29. builder.appendQueryParameter("uuid", params[1]);
  30. builder.appendQueryParameter("password", params[2]);
  31. String query = builder.build().getEncodedQuery();
  32. // Open connection for sending data
  33. OutputStream os = conn.getOutputStream();
  34. BufferedWriter writer = new BufferedWriter(
  35. new OutputStreamWriter(os, StandardCharsets.UTF_8));
  36. writer.write(query);
  37. writer.flush();
  38. writer.close();
  39. os.close();
  40. conn.connect();
  41. } catch (SocketTimeoutException e) {
  42. e.printStackTrace();
  43. // There is a problem with the main server, switch to backup server
  44. if (hostUrl.equals(mainUrl)) {
  45. return connect(backupUrl, params);
  46. }
  47. return "exception";
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. return "exception";
  51. }
  52. return "success";
  53. }
展开查看全部

相关问题