Unable to connect XMPP server with ASmack in Android

南笙酒味 提交于 2019-12-04 19:39:45
Manish Sindhal
public class ChatScreen extends Activity implements android.view.View.OnClickListener { 

private final static String SERVER_HOST = "192.168.88.158";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "192.168.88.158";
private final static String LOGIN = "manish";
private final static String PASSWORD = "android";
private List<String> m_discussionThread;
private ArrayAdapter<String> m_discussionThreadAdapter;
private XMPPConnection m_connection;
private Handler m_handler;
public static  ArrayList<HashMap<String, String>> usersList;
Presence presence;
ListView list;
int position = 0;

private Button BackBtn;
private Button EndChatBtn;
TextView opretor;
    String opretorName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.chat_screen);
    usersList=new ArrayList<HashMap<String, String>>();
    Intent intent = getIntent();
    opretorName = intent.getStringExtra("opName");
    opretor = (TextView) findViewById(R.id.Opretor1);
    opretor.setText(opretorName);
            m_handler = new Handler();
    try {
        initConnection();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    final EditText recipient = (EditText) this.findViewById(R.id.recipient);
    recipient.setText(opretorName + "@manishsys-pc");
    final EditText message = (EditText) this.findViewById(R.id.message);        
    list = (ListView) this.findViewById(R.id.thread);

    m_discussionThread = new ArrayList<String>();
    m_discussionThreadAdapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item, m_discussionThread);
    list.setAdapter(m_discussionThreadAdapter);

    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("SimpleDateFormat")
        public void onClick(View view) {
            String to = recipient.getText().toString();
            String text = message.getText().toString();

            Message msg = new Message(to, Message.Type.chat);
            Calendar c = Calendar.getInstance(); 
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMMM d, hh:mm a");
            String strdt= formatter.format(c.getTime());
            msg.setBody(text);
            m_connection.sendPacket(msg);
            m_discussionThread.add("me :" + strdt);
            m_discussionThread.add(text);
            m_discussionThreadAdapter.notifyDataSetChanged();

        }
    });

    BackBtn = (Button) findViewById(R.id.ChatBackBtn);
    BackBtn.setOnClickListener(this);
    EndChatBtn= (Button) findViewById(R.id.EndChatBtn);
    EndChatBtn.setOnClickListener(this);

}

private void initConnection() throws XMPPException {
    //Initialisation de la connexion
    ConnectionConfiguration config =
            new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME );
    m_connection = new XMPPConnection(config);
    m_connection.connect();
    m_connection.login(LOGIN, PASSWORD);
    presence = new Presence(Presence.Type.available);
    m_connection.sendPacket(presence);

    //Get all userList
    Roster roster = m_connection.getRoster();
    String[] urlArray = new String[] { "office" };
    roster.createEntry("ravi", "ravi", urlArray);
    Collection<RosterEntry> entries = roster.getEntries();

    for(RosterEntry entry : entries) {
        HashMap<String, String> map = new HashMap<String, String>();
        presence = roster.getPresence(entry.getUser());

        Presence.Type type = presence.getType();       

        map.put("USER", entry.getName().toString());
        map.put("STATUS", type.toString());
        Log.e("USER", entry.getName().toString());

        usersList.add(map);
    }

 // Assume we've created a Connection name "connection".
    ChatManager chatmanager = m_connection.getChatManager();
    Chat newChat = chatmanager.createChat("mansih@manishsys-pc ", new MessageListener() {
        public void processMessage(Chat chat, Message message) {
            System.out.println("Received message: " + message);
        }
    });

    try {
        newChat.sendMessage("Howdy!");
    }
    catch (XMPPException e) {
        System.out.println("Error Delivering block");
    }

    //enregistrement de l'écouteur de messages
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    m_connection.addPacketListener(new PacketListener() {
            @SuppressLint("SimpleDateFormat")
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Calendar c = Calendar.getInstance(); 
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMMM d, hh:mm a");
                    String strdt= formatter.format(c.getTime());
                    m_discussionThread.add(fromName + ":" + strdt);
                    m_discussionThread.add(message.getBody());

                    m_handler.post(new Runnable() {
                        public void run() {
                            list.smoothScrollToPosition(list.getCount()-1);
                            m_discussionThreadAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }
        }, filter);
    }
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.ChatBackBtn:

        Intent BackIntent = new Intent(ChatScreen.this,
                OperatorScreen.class);
        BackIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(BackIntent);
        ChatScreen.this.finish();

        break;
    case R.id.EndChatBtn:

        Intent EndChtIntent = new Intent(ChatScreen.this,
                RatingScreen.class);
        EndChtIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(EndChtIntent);
        ChatScreen.this.finish();

        break;

    default:
        break;`
    }
  }
}

it's my code it work fine for me i hope it helpfull for you. Thanks

try this

private final static String server_host = "talk.google.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "gmail.com";

ConnectionConfiguration config = new ConnectionConfiguration( server_host, SERVER_PORT , SERVICE_NAME);
        XMPPConnection m_connection = new XMPPConnection(config);
                try {
                     SASLAuthentication.supportSASLMechanism("PLAIN");
                     config.setSASLAuthenticationEnabled(true);     
                     m_connection.connect();
                    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
                } catch (XMPPException e) {
                    e.printStackTrace();
                }       

I found a solution to this issue. It was owing to the update I made to the API. I deleted the imported ASmack library from my project property. The Lib folder was missing too. Hence I made one folder ("libs") int he project and pasted the ASmack jar there. BY cleaning the project once, the error was fixed. Thank you. Reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!