2017-08-03 101 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

相關問題