我提前为格式化道歉。 我是Android开发的新手,我正在开发一个XMPP客户端,与家里的服务器交谈,通过Google Talk机器人做命令....
我将工作代码复制到我正在尝试制作的应用程序并给我错误...就像我说我是新的并且在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();
}
}
);
}
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());
}
}
}
}
按下按钮时出现此错误
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)
答案 0 :(得分:2)
Android 4.0或更高版本不允许在GUI线程中进行网络I / O操作,如果这样做,它将抛出NetworkOnMainThread
例外。为了防止这种情况,aSmack附带了AndroidConnectionConfiguration,它提供了
AndroidConnectionConfiguration(String serviceName)
AndroidConnectionConfiguration(String serviceName, int timeout)
AndroidConnectionConfiguration(String host, int port, String name)
,在使用时,将生成一个新线程,因此可以防止异常。
答案 1 :(得分:2)
仅当您尝试在主UI线程上调用长时间运行的进程(如Web服务调用)时,才会出现FYI android.os.NetworkOnMainThreadException
。它在< 2.3设备中效果最好,但是如果你还没有将它包含在线程中并尝试在> 3.0设备上运行它,它将为你提供NetworkOnMainThreadException。
要解决它,您可以实现AsyncTask(标准方式)或包含以下代码(懒惰方式):
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
检查并阅读有关NetworkOnMainThreadException的更多信息