国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

即時通訊之smack客戶端配置

2019-11-14 23:13:09
字體:
來源:轉載
供稿:網友
即時通訊之smack客戶端配置 2015-03-09 14:39 by 杰瑞教育, ... 閱讀, ... 評論, 收藏, 編輯

  之前學習了通過Openfire+spark+smack的模式來完成我們的即時通訊軟件,上次我們已經完成了Openfire的安裝和配置,這次我們繼續完成我們的客戶端部分。

  1.首先我們通過百度smack來下載我們所需要的jar包,將下載好的jar包導入到我們的工程中,創建一個工具類XmPPTool:

package com.xmpp.client.util;import org.jivesoftware.smack.ConnectionConfiguration;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.XMPPException;import org.json.JSONArray;public class XmppTool {    PRivate static XMPPConnection con = null;    private static void openConnection(String url, String pcName) {        try {            // url、端口,也可以設置連接的服務器名字,地址,端口,用戶。            ConnectionConfiguration connConfig = new ConnectionConfiguration(                    "192.168.2.113", 5222, "JEREHEDU");            con = new XMPPConnection(connConfig);            con.connect();        } catch (XMPPException xe) {            xe.printStackTrace();        }    }    private static void openOffConnection(String url, String pcName) {        try {            ConnectionConfiguration connConfig = new ConnectionConfiguration(                    url, 5222, pcName);            connConfig.setSendPresence(false);            con = new XMPPConnection(connConfig);            con.connect();        } catch (XMPPException xe) {            xe.printStackTrace();        }    }    public static XMPPConnection getOffConnection(String url, String pcName) {        if (con == null) {            openOffConnection(url, pcName);        }        return con;    }    public static XMPPConnection getConnection(String url, String pcName) {        if (con == null) {            openConnection(url, pcName);        }        return con;    }    public static void closeConnection() {        con.disconnect();        con = null;    }}
工具類XmppTool

  主要還是通過ConnectionConfiguration來連接服務器,傳入三個參數(地址,端口,用戶名)

  2.登陸界面

package com.xmpp.client;import java.util.Iterator;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smack.packet.Presence;import org.jivesoftware.smackx.OfflineMessageManager;import com.xmpp.client.util.XmppTool;import com.xmpp.client.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.Toast;public class FormLogin extends Activity implements OnClickListener {    private EditText useridText, pwdText, urlText, pcnameText;;    private LinearLayout layout1, layout2;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.formlogin);        // 獲取用戶和密碼        this.useridText = (EditText) findViewById(R.id.formlogin_userid);        this.pwdText = (EditText) findViewById(R.id.formlogin_pwd);        this.urlText = (EditText) findViewById(R.id.formlogin_url);        this.pcnameText = (EditText) findViewById(R.id.formlogin_pcname);        // 正在登錄        this.layout1 = (LinearLayout) findViewById(R.id.formlogin_layout1);        // 登錄界面        this.layout2 = (LinearLayout) findViewById(R.id.formlogin_layout2);        Button btsave = (Button) findViewById(R.id.formlogin_btsubmit);        btsave.setOnClickListener(this);        Button btcancel = (Button) findViewById(R.id.formlogin_btcancel);        btcancel.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // 根據ID來進行提交或者取消        switch (v.getId()) {        case R.id.formlogin_btsubmit:            // 取得填入的用戶和密碼            // 取得填入的用戶和密碼            final String USERID = this.useridText.getText().toString();            final String PWD = this.pwdText.getText().toString();            final String URL = this.urlText.getText().toString();            final String PCNAME = this.pcnameText.getText().toString();            Thread t = new Thread(new Runnable() {                public void run() {                    // sendEmptyMessage:發送一條消息                    handler.sendEmptyMessage(1);                    try {                        // 連接                        XmppTool.getOffConnection(URL, PCNAME).login(USERID,                                PWD);                        getOfflineMessage();                        // 狀態                        Presence presence = new Presence(                                Presence.Type.available);                        XmppTool.getConnection(URL, PCNAME)                                .sendPacket(presence);                        Intent intent = new Intent();                        intent.setClass(FormLogin.this, FormClient.class);                        intent.putExtra("USERID", USERID);                        intent.putExtra("URL", URL);                        intent.putExtra("PCNAME", PCNAME);                        FormLogin.this.startActivity(intent);                        FormLogin.this.finish();                    } catch (XMPPException e) {                        XmppTool.closeConnection();                        handler.sendEmptyMessage(2);                    }                }            });            t.start();            break;        case R.id.formlogin_btcancel:            finish();            break;        }    }    /**     * 獲取離線消息     */    private void getOfflineMessage() {        OfflineMessageManager offlineManager = new OfflineMessageManager(                XmppTool.getConnection(this.urlText.getText().toString(),                        this.pcnameText.getText().toString()));        Iterator<org.jivesoftware.smack.packet.Message> it;        try {            it = offlineManager.getMessages();            while (it.hasNext()) {                org.jivesoftware.smack.packet.Message message = it.next();                Log.i("離線消息", "收到離線消息, Received from 【" + message.getFrom()                        + "】 message: " + message.getBody());            }            // 刪除離線消息            offlineManager.deleteMessages();        } catch (XMPPException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            if (msg.what == 1) {                layout1.setVisibility(View.VISIBLE);                layout2.setVisibility(View.GONE);            } else if (msg.what == 2) {                layout1.setVisibility(View.GONE);                layout2.setVisibility(View.VISIBLE);                Toast.makeText(FormLogin.this, "登錄失敗!", Toast.LENGTH_SHORT)                        .show();            }        };    };}
登陸界面

  3.聊天客戶端

package com.xmpp.client;import java.io.File;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;import org.jivesoftware.smack.Chat;import org.jivesoftware.smack.ChatManager;import org.jivesoftware.smack.ChatManagerListener;import org.jivesoftware.smack.MessageListener;import org.jivesoftware.smack.PacketListener;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smack.packet.Message;import org.jivesoftware.smack.packet.Packet;import org.jivesoftware.smackx.filetransfer.FileTransfer;import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;import org.jivesoftware.smackx.filetransfer.FileTransferListener;import org.jivesoftware.smackx.filetransfer.FileTransferManager;import org.jivesoftware.smackx.filetransfer.FileTransferRequest;import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;import org.jivesoftware.smackx.muc.MultiUserChat;import org.jivesoftware.smackx.packet.DelayInformation;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.RequestParams;import com.lidroid.xutils.http.ResponseInfo;import com.lidroid.xutils.http.callback.RequestCallBack;import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;import com.xmpp.client.util.ScreenShot;import com.xmpp.client.util.TimeRender;import com.xmpp.client.util.XmppTool;public class FormClient extends Activity {    private MyAdapter adapter;    private List<Msg> listMsg = new ArrayList<Msg>();    private String pUSERID;    private EditText msgText, toText;    private ProgressBar pb;    private Button btsend;        private TextView waitpb;        private String URL;    private String PCNAME;    private String PERSON;    private Chat mychat;    private ChatManager cm;        public class Msg {        String userid;        String msg;        String date;        String from;        public Msg(String userid, String msg, String date, String from) {            this.userid = userid;            this.msg = msg;            this.date = date;            this.from = from;        }    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.formclient);        // 獲取Intent傳過來的用戶名,url,pcname        this.pUSERID = getIntent().getStringExtra("USERID");        this.URL = getIntent().getStringExtra("URL");        this.PCNAME = getIntent().getStringExtra("PCNAME");        ListView listview = (ListView) findViewById(R.id.formclient_listview);        listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);        this.adapter = new MyAdapter(this);        listview.setAdapter(adapter);        // 獲取文本信息        this.msgText = (EditText) findViewById(R.id.formclient_text);        // 獲取聊天對象        this.toText = (EditText) findViewById(R.id.formclient_to);                // 發送消息給water-pc服務器water(獲取自己的服務器,和好友)        // 消息監聽        cm = XmppTool.getConnection(URL, PCNAME).getChatManager();        chatListener();        // 發送消息        btsend = (Button) findViewById(R.id.formclient_btsend);        sendChatMessage();            }        /**     * send chat message     *      */    private void sendChatMessage() {        btsend.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // 獲取text文本                String msg = msgText.getText().toString();                PERSON = toText.getText().toString();                mychat = cm.createChat(PERSON + "@" + PCNAME, null);                if (msg.length() > 0) {                    // 發送消息                    listMsg.add(new Msg(pUSERID, msg, TimeRender.getDate(),                            "OUT"));                    // 刷新適配器                    adapter.notifyDataSetChanged();                    try {                        // 發送消息給指定人                        mychat.sendMessage(msg);                    } catch (XMPPException e) {                        e.printStackTrace();                    }                } else {                    Toast.makeText(FormClient.this, "請輸入信息", Toast.LENGTH_SHORT)                            .show();                }                // 清空text                msgText.setText("");            }        });    }    /**     * 監聽單人消息     */    private void chatListener() {        cm.addChatListener(new ChatManagerListener() {            @Override            public void chatCreated(Chat chat, boolean able) {                chat.addMessageListener(new MessageListener() {                    @Override                    public void processMessage(Chat chat2, Message message) {                        Log.v("--tags--", "--tags-form--" + message.getFrom());                        Log.v("--tags--",                                "--tags-message--" + message.getBody());                        // 收到來自water-pc服務器water的消息(獲取自己的服務器,和好友)                        if (message.getFrom().contains(pUSERID + "@keyi-pc")) {                            // 獲取用戶、消息、時間、IN                            String[] args = new String[] { pUSERID,                                    message.getBody(), TimeRender.getDate(),                                    "IN" };                           android.os.Message msg = handler.obtainMessage();                            msg.what = 1;                            msg.obj = args;                            msg.sendToTarget();                        } else {                                                                         }                            Log.i("json", "b  " + message.getBody());                            String amsg = message.getBody();                            String[] args = null;                                                        args = new String[] { message.getFrom(), amsg,                                        TimeRender.getDate(), "IN" };                                android.os.Message msg = handler                                        .obtainMessage();                                msg.what = 1;                                msg.obj = args;                                msg.sendToTarget();                                                }                                     });            }        });    }        private OutgoingFileTransfer fileTransfer;    Handler outHandler = new Handler();    Runnable outrunnable = new Runnable() {        @Override        public void run() {            if (fileTransfer.getProgress() == 1) {                pb.setVisibility(View.GONE);                outHandler.removeCallbacks(outrunnable);            }            outHandler.postDelayed(outrunnable, 100);        }    };        private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            switch (msg.what) {            case 1:                // 獲取消息并顯示                String[] args = (String[]) msg.obj;                listMsg.add(new Msg(args[0], args[1], args[2], args[3]));                // 刷新適配器                adapter.notifyDataSetChanged();                break;            case 2:                // 附件進度條                if (pb.getVisibility() == View.GONE) {                    pb.setVisibility(View.VISIBLE);                    waitpb.setVisibility(View.VISIBLE);                }                break;            case 3:                pb.setProgress(msg.arg1);                break;            case 4:                pb.setVisibility(View.GONE);                waitpb.setVisibility(View.GONE);                break;                                        default:                break;            }        };    };    // 退出    @Override    public void onBackPressed() {        super.onBackPressed();        XmppTool.closeConnection();        System.exit(0);        android.os.Process.killProcess(android.os.Process.myPid());    }        class MyAdapter extends BaseAdapter {        private Context cxt;        private LayoutInflater inflater;        public MyAdapter(FormClient formClient) {            this.cxt = formClient;        }        @Override        public int getCount() {            return listMsg.size();        }        @Override        public Object getItem(int position) {            return listMsg.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            // 顯示消息的布局:內容、背景、用戶、時間            this.inflater = (LayoutInflater) this.cxt                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            // IN,OUT的圖片            if (listMsg.get(position).from.equals("IN")) {                convertView = this.inflater.inflate(                        R.layout.formclient_chat_in, null);            } else {                convertView = this.inflater.inflate(                        R.layout.formclient_chat_out, null);            }            TextView useridView = (TextView) convertView                    .findViewById(R.id.formclient_row_userid);            TextView dateView = (TextView) convertView                    .findViewById(R.id.formclient_row_date);            TextView msgView = (TextView) convertView                    .findViewById(R.id.formclient_row_msg);            useridView.setText(listMsg.get(position).userid);            dateView.setText(listMsg.get(position).date);            msgView.setText(listMsg.get(position).msg);            return convertView;        }    }}
聊天客戶端

  其中有好多類都是自帶的我們只需要實現它就可以了。

作者:杰瑞教育出處:http://m.survivalescaperooms.com/jerehedu/本文版權歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇赉县| 博兴县| 皋兰县| 兰西县| 宜州市| 临沂市| 平南县| 康定县| 鄂州市| 上虞市| 广东省| 保德县| 南靖县| 同心县| 萨嘎县| 荆门市| 晋江市| 隆化县| 宁陕县| 平阴县| 承德市| 绥化市| 卓尼县| 政和县| 章丘市| 通化县| 泾阳县| 太仆寺旗| 太白县| 贡山| 红原县| 叶城县| 桑植县| 晴隆县| 东源县| 元江| 正定县| 屯门区| 湘乡市| 罗定市| 竹山县|