我提前为格式道歉。我是 android 开发的新手,我正在开发一个 XMPP 客户端,与家里的服务器交谈,通过谷歌谈话机器人执行命令....
- 我确实查看了stackoverflow的一些答案并搜索了一些答案
- 我通过 github 更新到最新的 ASMACK 库
- 我确实添加了使用权限
- 我让 ASMACK 在我的 android 上开发一个测试应用程序。
我将工作代码复制到我正在尝试制作的应用程序中并给了我错误......就像我说我是新手,10 年来没有做过 java 编程......我不知道如何听到错误堆栈
我有一个名为“设置对话框”的设置对话框。当我单击按钮时,我运行:
protected void onStart() {
super.onStart();
setContentView(R.layout.settings);
getWindow().setFlags(4, 4);
setTitle("XMPP Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String host = HOST_ADDRESS;
int port = HOST_PORT_NUM;
String service = HOST_SERVICE;
String username = "me@gmail.com";//getText(R.id.userid);
String password = "abc123";//getText(R.id.password);
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection XMPPCon = new XMPPConnection(connConfig);
try
{
XMPPCon.connect();
XMPPCon.login(username,password);
}
catch (XMPPException ex)
{
ex.printStackTrace();
HomeBotClient.setConnection(null);
}
HomeBotClient.setConnection(XMPPCon);
dismiss();
}
}
);
}
这在主要活动中称为“setConnection”函数
public void setConnection (XMPPConnection XMPPCon)
{
this.XMPPCon = XMPPCon;
if (XMPPCon != null)
{
ChatManager chatmanager = XMPPCon.getChatManager();
Chat newChat = chatmanager.createChat("psun03@gmail.com",new MessageListener()
{
public void processMessage(Chat chat, Message message) {
try
{
Log.v(TAG, "Got:" + message.getBody());
chat.sendMessage(message.getBody());
}
catch (XMPPException e)
{
e.printStackTrace();
Log.v(TAG, "Couldn't respond:" + e);
}
Log.v(TAG, message.toString());
}
});
try
{
newChat.sendMessage("OMNOMNOM");
}
catch (XMPPException e)
{
e.printStackTrace();
Log.v(TAG, "couldn't send:" + e.toString());
}
// Accept only messages from friend@gmail.com
PacketFilter filter
= new AndFilter(new PacketTypeFilter(Message.class),
new FromContainsFilter("psun03@gmail.com"));
// Collect these messages
PacketCollector collector = XMPPCon.createPacketCollector(filter);
while(true) {
Packet packet = collector.nextResult();
if (packet instanceof Message)
{
Message msg = (Message) packet;
// Process message
Log.v(TAG, "Got message:" + msg.getBody());
}
}
}
}
按下按钮时出现此错误
04-07 17:21:15.011: E/AndroidRuntime(1099): FATAL EXCEPTION: main
04-07 17:21:15.011: E/AndroidRuntime(1099): android.os.NetworkOnMainThreadException
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:49)
04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:576)
04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1034)
04-07 17:21:15.011: E/AndroidRuntime(1099): at com.ps.homebotclient.SettingsDialog$1.onClick(SettingsDialog.java:53)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.view.View.performClick(View.java:4204)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.view.View$PerformClick.run(View.java:17355)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Handler.handleCallback(Handler.java:725)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:137)
04-07 17:21:15.011: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-07 17:21:15.011: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 17:21:15.011: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 17:21:15.011: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-07 17:21:15.011: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-07 17:21:15.011: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)