长连接

发送图片

图片放在asset目录下


public void sendImage(View view)
    {
    try {
        //读出图
        AssetManager manager=getAssets();
        InputStream is=
                manager.open( "first_logo.png");
        //把图转成文本
        int size=is.available();
        byte[] data=new byte[size];
        is.read(data);
        //字符编码 0001 a  0008 a
        //String str=new String(data);
        //好友iphone 6 plus 收到后
        //str.getBytes();
        String body=Base64.encodeToString(data,
                Base64.DEFAULT);
        //加上信息表示发的是图
        body="image"+body;
        //发送
        TApplication.multiuserchat
        .sendMessage(body);
    } catch (Exception e) {
        
    }    

    }

发送文字

sendbtn = (Button) findViewById(R.id.send);
        bodyet = (EditText) findViewById(R.id.body);
        myreceive = new Myreceive();
        this.registerReceiver(myreceive, new IntentFilter("show"));

        linearlayout = (LinearLayout) findViewById(R.id.ll_chatContent);
        sendbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    String body = bodyet.getText().toString();
                    TApplication.multiuserchat.sendMessage("android" + body);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

TApplication中的程序

new Thread(){
            public void run() {

                try {
                    ConnectionConfiguration config=new ConnectionConfiguration(
                            "172.60.6.73",5222,"tarena.com");
                    xmpp=new XMPPConnection(config);
                    MyPacklistener packetListener=new MyPacklistener();
                    xmpp.addPacketListener(packetListener, null);
                    
                    xmpp.connect();
                    
                    xmpp.login("juexinbugai", "lizhenjing");
                    //加入群聊
                    multiuserchat=new MultiUserChat(xmpp, "gsd1602@conference.tarena.com");
                    multiuserchat.join("1213142");
                    //在android加入成功,电脑的spark上能看到
                } catch (Exception e) {
                    
                    e.printStackTrace();
                }

            };
        }.start();

class MyPacklistener implements PacketListener{

        @Override
        public void processPacket(Packet packet) {
            if (packet instanceof Message) {
                Message msg=(Message) packet;
                String who=msg.getFrom();
                String body=msg.getBody();
                //发广播
                Intent intent=new Intent("show");
                intent.putExtra("who", who);
                intent.putExtra("body", body);
                
                sendBroadcast(intent);
                
            }
        }
        
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值