无法登录到服务器开火

时间:2013-11-23 20:28:44

标签: android xmpp openfire

我正在创建一个即时通讯应用程序,使用此代码下面的doAllconnections方法,我可以通过简单的界面连接和聊天,只需编辑文本,textview和发送按钮。但是现在我正在尝试改进它并将传入或接收的消息保存在android数据库中并使用列表视图来显示它们。但是当doAllConnections到达connection.login时它从不登录也没有任何错误。我使用开火作为服务器。请帮助!

public class MainActivity extends Activity { 
SqlHandler sqlHandler; 
ListView lvCustomList; 
EditText enter_message; 
ImageButton btn_send; 
TextView umf_tv_msgs_display, umf_tv_nname;
private XMPPConnection connection;  
ChatManager chatmanager;
Chat newChat;

@Override
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main); 
lvCustomList = (ListView) findViewById(R.id.lv_custom_list); 
enter_message = (EditText) findViewById(R.id.enter_message);  
btn_send = (ImageButton) findViewById(R.id.imageButton2);

// inflate upper_main_framelayout
//FrameLayout f = (FrameLayout) findViewById(R.id.upper_main_framelayout);
//LayoutInflater l = getLayoutInflater();
//View v = l.inflate(R.layout.upper_main_framelayout_xml, null);
//f.addView(v);

// get layout components of upper_main_framelayout into java
umf_tv_msgs_display = (TextView) findViewById(R.id.tv_msgs_display);
umf_tv_nname = (TextView) findViewById(R.id.tv_nname);

// get instance of sqlHandler (help with running sql query), then ask showlist to setup listview 
sqlHandler = new SqlHandler(this); 
showList(); 

// set chat connection by calling doAllconnections
Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            doAllconnections();
        } catch (Exception e) {

        }
    }
});
thread.start();

// send button handler
btn_send.setOnClickListener(new OnClickListener() { 

    @Override
    public void onClick(View v) { 

        if (enter_message.getText().toString().isEmpty()){

        }
        else {
            // get date and time in the stated format and put message in msg variable
            Calendar c = Calendar.getInstance();
            SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
            SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
            String date = dateformat.format(c.getTime());
            String time = timeformat.format(c.getTime());
            String msg = enter_message.getText().toString(); 

            // send message
            try {
                    newChat.sendMessage(enter_message.getText().toString());
                    runOnUiThread(new Runnable() {
                        public void run() {
                            umf_tv_msgs_display.setText("Message Sent!"); 
                             } });
                } 
            catch (XMPPException e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            umf_tv_msgs_display.setText("Message Delivery failed!"); } });
                }
          // store SENT message in database and refresh listview  
        String query = "INSERT INTO tbl_nickname(time,msg,date) values ('"+ time + "','" + msg + "','" + date + "')"; 
        sqlHandler.executeQuery(query); 
        showList(); 
        enter_message.setText(""); 
    }  
  }
});  
} 

// handles all the listview updating and adding
private void showList() { 

ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>(); 
contactList.clear(); 
String query = "SELECT * FROM tbl_nickname"; 
Cursor c1 = sqlHandler.selectQuery(query); 

if (c1 != null && c1.getCount() != 0) { 
    if (c1.moveToFirst()) { 
        do { 
            ContactListItems contactListItems = new ContactListItems(); 
            contactListItems.settime(c1.getString(c1.getColumnIndex("time"))); 
            contactListItems.setmsg(c1.getString(c1.getColumnIndex("msg"))); 
            contactListItems.setdate(c1.getString(c1.getColumnIndex("date"))); 
            contactList.add(contactListItems); 
        } while (c1.moveToNext()); 
    } 
} 
c1.close(); 
ContactListAdapter contactListAdapter = new ContactListAdapter(MainActivity.this, contactList); 
lvCustomList.setAdapter(contactListAdapter); 
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 

        // chat server connections
public void doAllconnections(){  

     ConnectionConfiguration connConfig = new     ConnectionConfiguration("192.168.1.101", 5222);
     connConfig.setSASLAuthenticationEnabled(false);

     connection = new XMPPConnection(connConfig);

        // Connect to server
        try {
            connection.connect();

            runOnUiThread(new Runnable() {
                public void run() {
                    umf_tv_msgs_display.setText("Connected to " + connection.getHost());    } });
        }
            catch (XMPPException ex) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        umf_tv_msgs_display.setText("Failed to connect " + connection.getHost());   }});
            }

        // Login to server
        runOnUiThread(new Runnable() {
            public void run() {
                umf_tv_msgs_display.setText("Logging in...");   }});

        try {
            Log.d("MESSAGE!!!", "before login!");
            connection.login("abc1", "windows");
            Log.d("MESSAGE!!!", "at login!");

            runOnUiThread(new Runnable() {
                public void run() {
                    umf_tv_msgs_display.setText("Logged in as " + connection.getUser());    }});

            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);

        } catch (XMPPException ex) {
            runOnUiThread(new Runnable() {
                public void run() {
                    umf_tv_msgs_display.setText("Failed to log in as " + connection.getUser()); }});
        }

        // Setup chat and receiver
        chatmanager = connection.getChatManager(); 
        newChat = chatmanager.createChat("abc2@emonet", new MessageListener() {
            public void processMessage(Chat chat, final Message message) {

                runOnUiThread(new Runnable() {
                    public void run() {

        // get date and time in the stated format: will use phone receiving time/date as received time/date
            Calendar c = Calendar.getInstance();
            SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
            SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
            String date = dateformat.format(c.getTime());
            String time = timeformat.format(c.getTime());           
            String received_msg = message.getBody().toString();

        // store RECEIVED message in database and refresh listview  
            String query = "INSERT INTO tbl_nickname(time,msg,date) values ('"+ time + "','" + received_msg + "','" + date + "')"; 
            sqlHandler.executeQuery(query); 
            showList(); 
            umf_tv_msgs_display.setText("Received message!"); }});
                }});
        }

}

0 个答案:

没有答案