Android 開發即時聊天工具 YQ :(三) 實現登陸功能

前面socket基本通信完瞭,登陸界面也已經完成,下面就是重點瞭,實現登陸功能

服務器和客戶端的代碼當然不肯能用那個控制臺的那個瞭,所以全部得重寫,不過原理都一樣,代碼也差不多,都有註釋,一看就明白。

先是登陸的Activity:

[java] 
public class LoginActivity extends Activity { 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_login); 
        Button btnLogin=(Button) findViewById(R.id.btn_login); 
        btnLogin.setOnClickListener(new OnClickListener(){ 
            public void onClick(View arg0) { 
                int account=Integer.parseInt(((EditText) findViewById(R.id.et_account)).getText().toString()); 
                String password=((EditText) findViewById(R.id.et_password)).getText().toString(); 
                login(account, password); 
            } 
        });  
    } 
     
    void login(int a, String p){ 
        User user=new User(); 
        user.setAccount(a); 
        user.setPassword(p); 
        user.setOperation("login"); 
        boolean b=new YQConServer().sendLoginInfo(user); 
        //登陸成功 
        if(b){ 
            try { 
                //發送一個要求返回在線好友的請求的Message 
<span style="white-space:pre">              </span>//— 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
            //轉到主界面, 
            Intent i=new Intent(this, MainActivity.class); 
            startActivity(i); 
        }else { 
            Toast.makeText(this, "登陸失敗!不告訴你為什麼,", Toast.LENGTH_SHORT).show(); 
        } 
    } 

將登陸的信息封裝到user中,user的operation用來判斷該user包的類型,交由YQClient來發送到服務器。

客戶端:

[java]
public class YQClient{ 
    public Socket s; 
     
    public boolean sendLoginInfo(Object obj){ 
        boolean b=false; 
        try { 
            s=new Socket(); 
            try{ 
                s.connect(new InetSocketAddress("10.0.2.2",5469),2000); 
            }catch(SocketTimeoutException e){ 
                //連接服務器超時 
                return false; 
            } 
            ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream()); 
            oos.writeObject(obj); 
            ObjectInputStream ois=new ObjectInputStream(s.getInputStream()); 
            YQMessage ms=(YQMessage)ois.readObject(); 
            if(ms.getType().equals(YQMessageType.SUCCESS)){ 
                //創建一個該賬號和服務器保持連接的線程 
                //— 
                b=true; 
            }else if(ms.getType().equals(YQMessageType.FAIL)){ 
                b=false; 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } catch (ClassNotFoundException e) { 
            e.printStackTrace(); 
        } 
        return b; 
    } 

在登陸成功後,將會新開一個線程和服務器保持連接,該線程將用來通信,

要捕獲SocketTimeoutException異常,否則連接 不到服務器,app會無響應,這裡設置2s服務器無響應,則連接服務器超時。

 

最後在看服務器端:

[java]
public class YQServer { 
    public YQServer(){ 
        ServerSocket ss = null; 
        try { 
            ss=new ServerSocket(5469); 
            System.out.println("服務器已啟動,正在監聽5469端口…"); 
            while(true){ 
                Socket s=ss.accept(); 
                //接受客戶端發來的信息 
                ObjectInputStream ois=new ObjectInputStream(s.getInputStream()); 
                User u=(User) ois.readObject(); 
                YQMessage m=new YQMessage(); 
                ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream()); 
                 
                if(u.getOperation().equals("login")){ //登錄 
                    int account=u.getAccount(); 
                    boolean b=new UserDao().login(account, u.getPassword());//連接數據庫驗證用戶 
                    if(b){ 
                        System.out.println("["+account+"] 上線瞭!"); 
                        m.setType(YQMessageType.SUCCESS);//返回一個成功登陸的信息包 
                        oos.writeObject(m); 
                        //單開一個線程,讓該線程與該客戶端保持連接 
                        //— 
                    }else{ 
                        m.setType(YQMessageType.FAIL); 
                        oos.writeObject(m); 
                    } 
                }else if(u.getOperation().equals("register")){ 
                    //註冊 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }    
    } 
 

最後測試一下:

 
 

You May Also Like