2017-08-03 100 views
0

我正在构建一个使用Java服务器(在计算机上)连接的Android应用程序。 我有一个问题 - 当我发现服务器没有连接时,我试图重新连接到服务器,但它不起作用。 下面是客户端类代码:Android套接字重新连接到服务器

public class Client extends AsyncTask { 

    private final int port = 1978; 
    private final String ip = "192.168.14.22"; 
    private Socket socket; 
    private DataOutputStream output; 
    private DataInputStream input; 


    public Client() { 

    } 

    @Override 
    protected Object doInBackground(Object[] objects) { 
     try { 
      socket = new Socket(ip, port); 
      output = new DataOutputStream(socket.getOutputStream()); 
      input = new DataInputStream(socket.getInputStream()); 

      Log.d("Network c1", "Connected"); 
     } catch (IOException e) { 
      socket = null; 
      Log.d("Network c1", "Not connected"); 
     } 
     return null; 
    } 

    public boolean checkConnection() { 
     if (output == null) 
      return false; 
     try { 
      output.writeUTF("abc"); 
      return true; 
     } catch (IOException e) { 
      return false; 
     } 
    } 

    @Override 
    protected void onProgressUpdate(Object[] values) { 

    } 

} 

以及活动代码:

public class LogInActivity extends AppCompatActivity { 

    Client client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_log_in); 

     client = new Client(); 
     client.execute(); 

     //I used timer because it didn't work without it- That saied always 'not connected' message/Toast 
     new CountDownTimer(5, 0) { 

      public void onTick(long millisUntilFinished) { 

      } 

      public void onFinish() { 
       check(); 
      } 
     }.start(); 
    } 

    private void check() { 
     boolean isProcess; 

     isProcess = !checkConnection(); 

     if (isProcess) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog_Alert); 
      builder.setTitle(getResources().getString(R.string.app_name)); 
      builder.setMessage("Unable connect to the library"); 
      builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        //See note 1. 
        check(); 
       } 
      }); 
      builder.setCancelable(false); 
      builder.show(); 
     } 
    } 

    public boolean checkConnection() { 
     if (client.checkConnection()) { 
      Toast.makeText(getApplicationContext(), "Connected to the library", Toast.LENGTH_SHORT).show(); 
      return true; 
     } else { 
      Toast.makeText(this, "Unable connect to the library", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    } 
} 

注1: 的问题是在这里。

此对话框需要显示,直到服务器/库连接。

如果服务器打开应用程序之前,check()方法运行良好,并说'连接成功',并且对话框不显示。

但是,如果应用程序启动时,服务器无法访问,并且稍后打开(并且变得可以访问) - check()方法不起作用并始终显示对话框。

什么问题?

顺便说一句,我试图重新启动客户端AsyncTask Class,但我没有成功。 (我试图做close(true)它,做excute()后一遍,但cancel()方法没有工作,并且是一个错误说,经过excuted一个AsyncTask Class,它不能再EXCUTE)

谢谢。

+0

你在哪里试图重新连接? – EJP

+0

我试图再次执行,但它不工作。我怎样才能以另一种方式重新连接? –

回答

0

您不应该定期检查连通性(每隔几秒钟就像您​​在此代码中做的那样)。相反,你应该让操作系统为你做到这一点,它将在电池和CPU方面更可靠和更高效。

看一看this answer

相关问题