采用拍击API和talk.goolgle.com服务器安卓聊天服务器、talk、API、com

2023-09-06 23:04:27 作者:终于到了小时候

我想用嫌API和创建一个Android聊天应用程序的 talk.google.com 服务器,但问题是,当我创建一个连接使用Gmail ID和密码嫌API和检查我的用户使用connection.getuser()我的id是showm在下面的格式名称anything@gmail.com/Smack_的的 (有的嫌ID)。现在,如果我发送邮件从我的应用程序用另一部电话anything@gmail.com它没有收到,但如果我把它给anything@gmail.com/Smack 的 _ (同嫌ID)这是正确的:收到。

该咂嘴ID被附加是随机变化,每次我连接,因此是不可能的,我以任何方式获取它,因此消息无法发送,这样我怎么能解决这个问题。

在code我ChatActivity是:

 包com.prince.chat;

进口的java.util.ArrayList;
进口java.util.Collection的;

进口org.jivesoftware.smack.ConnectionConfiguration;
进口org.jivesoftware.smack.PacketListener;
进口org.jivesoftware.smack.Roster;
进口org.jivesoftware.smack.RosterEntry;
进口org.jivesoftware.smack.XMPPConnection;
进口org.jivesoftware.smack.XMPPException;
进口org.jivesoftware.smack.filter.MessageTypeFilter;
进口org.jivesoftware.smack.filter.PacketFilter;
进口org.jivesoftware.smack.packet.Message;
进口org.jivesoftware.smack.packet.Packet;
。进口org.jivesoftware.smack.packet presence;
进口org.jivesoftware.smack.util.StringUtils;

进口android.os.Bundle;
进口android.os.Handler;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ListView;

公共类ChatActivity延伸活动{

    公共静态最后弦乐HOST =talk.google.com;
    公共静态最终诠释端口= 5222;
    公共静态最后弦乐SERVICE =gmail.com;
    公共静态字符串用户名;
    公共静态字符串密码;

    私人XMPPConnection连接;
    私人的ArrayList<字符串>消息=新的ArrayList<字符串>();
    私人处理程序mHandler =新的处理程序();

    私人的EditText收件人;
    私人的EditText文字信息;
    私人的ListView列表视图;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_chat);

        收件人=(EditText上)this.findViewById(R.id.toET);
        文字信息=(EditText上)this.findViewById(R.id.chatET);
        列表视图=(ListView控件)this.findViewById(R.id.listMessages);
        setListAdapter();

        意图I = getIntent();
        USERNAME = i.getStringExtra(电子邮件);
        PASSWORD = i.getStringExtra(密码);

        //设置一个监听器发送聊天短信
        按钮发送=(按钮)this.findViewById(R.id.sendBtn);
        send.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图查看){
                。字符串= recipient.getText()的toString();
                字符串文本= textMessage.getText()的toString()。
                Log.i(XMPPChatDemoActivity,发送文本+文字+到+到);
                消息味精=新的消息(于Message.Type.chat);
                msg.setBody(文本);
                如果(连接!= NULL){
                    connection.sendPacket(MSG);
                    messages.add(connection.getUser()+:);
                    messages.add(文本);
                    setListAdapter();
                }
            }
        });
        连接();
    }



    / **
     *通过调用设置对话框,当连接establised与
     * XMPP服务器
     * /
    公共无效setConnection(XMPPConnection连接){
        this.connection =连接;
        如果(连接!= NULL){
            //添加一个数据包监听器被发送给我们的消息
            PacketFilter过滤器=新MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(新PacketListener(){
                @覆盖
                公共无效processPacket(分组数据包){
                    消息消息=(消息)数据包;
                    如果(message.getBody()!= NULL){
                        串fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i(XMPPChatDemoActivity,文字:收到+ message.getBody()+由+ fromName);
                        messages.add(fromName +:);
                        messages.add(message.getBody());
                        //添加进来的邮件列表视图
                        mHandler.post(新的Runnable(){
                            公共无效的run(){
                                setListAdapter();
                            }
                        });
                    }
                }
            }, 过滤);
        }
    }

    私人无效setListAdapter(){
        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.listitem,消息);
        listview.setAdapter(适配器);
    }

    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        尝试 {
            connection.disconnect();
        }赶上(例外五){

        }
    }

    公共无效连接(){

        最后ProgressDialog对话框= ProgressDialog.show(这一点,连接......,请稍候...,假);
        线程t =新主题(新的Runnable(){
            @覆盖
            公共无效的run(){
                //创建一个连接
                ConnectionConfiguration connConfig =新ConnectionConfiguration(主机,端口,服务);
                XMPPConnection连接=新XMPPConnection(connConfig);
                尝试 {
                    connection.connect();
                    Log.i(XMPPChatDemoActivity,[SettingsDialog]连接到+ connection.getHost());
                }赶上(XMPPException前){
                    Log.e(XMPPChatDemoActivity,[SettingsDialog]无法连接到+ connection.getHost());
                    Log.e(XMPPChatDemoActivity,ex.toString());
                    setConnection(空);
                }
                尝试 {
                    connection.login(用户名,密码);
                    Log.i(XMPPChatDemoActivity,已登录为+ connection.getUser());

                    //设置状态可用
                    presence presence =新的presence(presence.Type.available);
                    connection.sendPacket(presence);
                    setConnection(连接);

                    名册名册= connection.getRoster();
                    收藏< RosterEntry>条目= roster.getEntries();
                    对于(RosterEntry项:项){

                        Log.d(XMPPChatDemoActivity,--------------------------------------);
                        Log.d(XMPPChatDemoActivity,RosterEntry+条目);
                        Log.d(XMPPChatDemoActivity,用户:+ entry.getUser());
                        Log.d(XMPPChatDemoActivity,名称:+ entry.getName());
                        Log.d(XMPPChatDemoActivity,状态:+ entry.getStatus());
                        Log.d(XMPPChatDemoActivity,类型:+ entry.getType());
                        presence入门presence = roster.get presence(entry.getUser());

                        Log.d(XMPPChatDemoActivity,presence状态:+入门presence.getStatus());
                        Log.d(XMPPChatDemoActivity,presence类型:+条目presence.getType());

                        presence.Type型=条目presence.getType();
                        如果(类型== presence.Type.available)
                            Log.d(XMPPChatDemoActivity,presence可用的编缉);
                        Log.d(XMPPChatDemoActivity,presence:+条目presence);
                    }
                }赶上(XMPPException前){
                    Log.e(XMPPChatDemoActivity,无法为登录+用户名);
                    Log.e(XMPPChatDemoActivity,ex.toString());
                    setConnection(空);
                }
                dialog.dismiss();
            }
        });
        t.start();
        dialog.show();
    }



    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.chat,菜单);
        返回true;
    }

}
 

解决方案

您可以将资源ID的一些连接时已知的值,所以没有生成的ID将被使用(除非服务器更改它,我相信它可以做,如果希望这样)。

Google Talk 是什么 Google Talk 怎么用

I want to create an android chat application using smack api and talk.google.com server but the problem is that when I create a connection to Smack API using gmail id and password and check my user name using "connection.getuser()" my id is showm in following format "anything@gmail.com/Smack_(some smack id)".Now if I send a message from my app using another phone to "anything@gmail.com" it is not recieved but if I send it to "anything@gmail.com/Smack_(same smack id)" it is properly recieved.

The smack id being appended is random and changes everytime I connect so it is impossible for me to obtain it by any means and thus message cannot be sent,so how can I overcome this problem.

The code for my ChatActivity is:

    package com.prince.chat;

import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatActivity extends Activity {

    public static final String HOST = "talk.google.com";
    public static final int PORT = 5222;
    public static final String SERVICE = "gmail.com";
    public static  String USERNAME ;
    public static  String PASSWORD ;

    private XMPPConnection connection;
    private ArrayList<String> messages = new ArrayList<String>();
    private Handler mHandler = new Handler();

    private EditText recipient;
    private EditText textMessage;
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        recipient = (EditText) this.findViewById(R.id.toET);
        textMessage = (EditText) this.findViewById(R.id.chatET);
        listview = (ListView) this.findViewById(R.id.listMessages);
        setListAdapter();

        Intent i = getIntent();
        USERNAME= i.getStringExtra("email");
        PASSWORD =i.getStringExtra("password");

        // Set a listener to send a chat text message
        Button send = (Button) this.findViewById(R.id.sendBtn);
        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String to = recipient.getText().toString();
                String text = textMessage.getText().toString();          
                Log.i("XMPPChatDemoActivity ", "Sending text " + text + " to " + to);
                Message msg = new Message(to, Message.Type.chat);  
                msg.setBody(text);
                if (connection != null) {
                    connection.sendPacket(msg);
                    messages.add(connection.getUser() + ":");
                    messages.add(text);
                    setListAdapter();
                }
            }
        });
        connect();
    }



    /**
     * Called by Settings dialog when a connection is establised with 
     * the XMPP server
     */
    public void setConnection(XMPPConnection connection) {
        this.connection = connection;
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                @Override
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " +  fromName);
                        messages.add(fromName + ":");
                        messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() {
                            public void run() {
                                setListAdapter();
                            }
                        });
                    }
                }
            }, filter);
        }
    }

    private void setListAdapter() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, messages);
        listview.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            connection.disconnect();
        } catch (Exception e) {

        }
    }

    public void connect() {

        final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // Create a connection
                ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
                XMPPConnection connection = new XMPPConnection(connConfig);
                try {
                    connection.connect();
                    Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+connection.getHost());
                } catch (XMPPException ex) {
                    Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
                    Log.e("XMPPChatDemoActivity", ex.toString());
                    setConnection(null);
                }
                try {
                    connection.login(USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity",  "Logged in as" + connection.getUser());

                    // Set the status to available
                    Presence presence = new Presence(Presence.Type.available);
                    connection.sendPacket(presence);
                    setConnection(connection);

                    Roster roster = connection.getRoster();
                    Collection<RosterEntry> entries = roster.getEntries();
                    for (RosterEntry entry : entries) {

                        Log.d("XMPPChatDemoActivity",  "--------------------------------------");
                        Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                        Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
                        Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
                        Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
                        Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
                        Presence entryPresence = roster.getPresence(entry.getUser());

                        Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
                        Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());

                        Presence.Type type = entryPresence.getType();
                        if (type == Presence.Type.available)
                            Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                        Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
                    }
                } catch (XMPPException ex) {
                    Log.e("XMPPChatDemoActivity", "Failed to log in as "+  USERNAME);
                    Log.e("XMPPChatDemoActivity", ex.toString());
                    setConnection(null);
                }
                dialog.dismiss();
            }
        });
        t.start();
        dialog.show();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.chat, menu);
        return true;
    }

}

解决方案

You can set the resource id to some known value when you connect, so no generated id will be used (unless the server changes it, which I believe it can do if it wishes).