基于XMPP和openfire(服务器)实现即时通讯中的用户登录功能

基于XMPP和openfire(服务器)实现即时通讯中的用户登录功能

初学Android   现在想利用openfire作为服务器,基于XMPP协议写一个即时聊天的android应用,
现在整理一下实现即时通讯中的用户登录功能,有不当之处,敬请指出,共同学习进步。

** XMPP实现简单登录功能(利用openfire作为服务器) **
用户登录可能会出现登录不上,出现异常的情况,所以登录这一部分就需要try和catch一下
以下是我的思维步骤:

Try{
1. 创建连接配置对象config
a) 用到的类:ConnectionConfiguration(HOST, PORT);
b) HOST是主机ip地址,windows系统在cmd命令行里输入ipconfig可以查询到ipv4
c) PORT是服务器占用的端口号(openfire的PORT为5222)
2.对连接配置对象进行配置
a)调用config的setSecurity(ConnectConfiguration.SecurityMode.disabled);
//即禁用安全模式,明文传输
b)开启调试模式,config.setDebuggerEnable(true);
3.创建XMPP连接对象
XMPPConnection conn = new XMPPConnection(config)
4.开始连接服务器(此时并没有登录上服务器,只能说这个时候conn对象能与服务器之间传送数据)
conn.connect();
5.登录服务器
conn.login(username, password);//用户名和密码
}
catch(XMPPException e){
e.printStackTrace();
//提示连接失败
Toast一下就可以了。
}

LoginActivity.java:

package com.example.luhui.lizi20160831;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import utils.PreferencesUtils;
import utils.ThreadUtils;
import utils.ToastUtils;

public class LoginActivity extends Activity {
    private static final String HOST = "192.168.2.100";//在这里改成自己的ip
    private static final int PORT = 5222;

    private EditText et_account , et_password;
    private Button login ;
    String account , password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
    }

    private void initListener() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = et_account.getText().toString();
                password = et_password.getText().toString();

                if (TextUtils.isEmpty(account)) {
                    ToastUtils.showToast(LoginActivity.this, "账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtils.showToast(LoginActivity.this, "密码不能为空");
                    return;
                }
                ThreadUtils.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);
                            /*
                            额外的配置
                             */
                            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);//禁用安全模式,明文传输
                            config.setDebuggerEnabled(true);//开启调试模式
                            //创建链接对象
                            XMPPConnection conn = new XMPPConnection(config);
                            //开始链接
                            conn.connect();
                            //登录
                            conn.login(account, password);
                            //链接成功
                            ToastUtils.showToast(LoginActivity.this, "登录成功");
                            Intent intent = new Intent(LoginActivity.this, ContactActivity.class);
                            startActivity(intent);
                            finish();
                        } catch (XMPPException e) {
                            e.printStackTrace();
                            //提示链接失败
                            ToastUtils.showToast(LoginActivity.this, "登录失败,请检查账号和密码是否正确");
                        }
                    }
                });
            }
        });
    }

    private void initView() {
        et_password = (EditText) findViewById(R.id.et_password);
        et_account = (EditText)findViewById(R.id.et_account);
        login = (Button) findViewById(R.id.btn_login);
    }
}

activity_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.luhui.lizi20160831.LoginActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <ImageView
            android:id="@+id/img_login"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/login_default_avatar"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_marginTop="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号: "
                android:textSize="20dp"/>
            <EditText
                android:id="@+id/et_account"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="请输入账号"
                android:text="luhui"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码: "
                android:textSize="20dp"/>
            <EditText
                android:id="@+id/et_password"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="请输入密码"
                android:text="123456"
                android:password="true"/>
        </LinearLayout>
        <Button
            android:id="@+id/btn_login"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_login_selector"
            android:layout_marginTop="15dp"
            android:text="登录"
            android:textSize="28dp"
            android:textColor="#ffffff"/>
    </LinearLayout>

</RelativeLayout>

Android Studio工程百度云链接:http://pan.baidu.com/s/1eSOCrx4


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章