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

來源:互聯網
上載者:User

前面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(); 
        }    
    } 
 

最後測試一下:

 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.