Android聊天在文本视图中打印传入消息

时间:2013-09-14 10:53:33

标签: android android-asynctask smack ejabberd

我正在使用ejabberd和smack创建一个聊天应用程序。我已成功登录服务器。现在我陷入了在文本视图中打印传入消息的困境。我可以看到消息即将使用日志。但我没有在文本视图中看到输出。请参阅以下代码。

import java.util.Collection;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    static final int CONNECTION_FAILURE = 0;
    static final int LOGIN_FAILURE = 1;
    static final int SUCCESS = 2;
    XMPPConnection connection;
    ChatManager chatmanager;
    Chat newChat;
    Button send;
    EditText sendMessageBox;
    public TextView chatHistoryBox;
    String user1 = "admin";
    String user2 = "test";

    String message = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        login();
        send = (Button) findViewById(R.id.buttonSend);
        sendMessageBox = (EditText) findViewById(R.id.editTextSendMessage);
        chatHistoryBox = (TextView) findViewById(R.id.textViewChatHistory);

    }

    public void login() {
        new Connection().execute(user1, "1234");
    }

    private class Connection extends AsyncTask<String, Void, Integer> {

        @Override
        protected Integer doInBackground(String... strings) {
            ConnectionConfiguration conConfig = new ConnectionConfiguration(
                    "192.168.2.6", 5222, "localhost");
            connection = new XMPPConnection(conConfig);
            try {
                connection.connect();

                Log.i("AppName 1", "CONNECTED TO " + connection.getHost());
                Log.e("Service Name", connection.getServiceName());
            } catch (Exception e) {
                Log.e("AppName 2", e.getMessage());
                return CONNECTION_FAILURE;
            }
            try {
                connection.login(strings[0], strings[1]);
                Log.i("AppName 3", "LOGGED IN AS " + connection.getUser());
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);

            } catch (Exception e) {
                Log.e("AppName 4", e.toString());
                return LOGIN_FAILURE;
            }
            try {
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {
                    System.out.println(entry);
                }

            } catch (Exception e) {
                Log.e("AppName 5", e.toString());
            }

            return SUCCESS;
        }

        @Override
        protected void onPostExecute(Integer result) {
            if (result.equals(SUCCESS)) {
                chatmanager = connection.getChatManager();
                newChat = chatmanager.createChat(user2 + "@localhost",
                        new MessageListener() {

                            @Override
                            public void processMessage(Chat chat, Message msg) {

                                chatHistoryBox.setText(msg.getBody());
                                Log.wtf("AppName 5.5 ", msg.getBody());



                            }
                        });

                send.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            newChat.sendMessage(sendMessageBox.getText()
                                    .toString());
                        } catch (Exception e) {
                            Log.e("AppName 6", e.toString());
                        }

                    }
                });
            }



        }

    }

}

如何将传入的消息放入文本视图?

1 个答案:

答案 0 :(得分:2)

试试这种方式

宣布汉德勒

private Handler mHandler = new Handler();


newChat = chatmanager.createChat(user2 + "@localhost",
                new MessageListener() {

                    @Override
                    public void processMessage(Chat chat, Message msg) {

                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                chatHistoryBox.setText(msg.getBody());
                            }
                        });
                        Log.wtf("AppName 5.5 ", msg.getBody());

                    }
                });