这是我通过aSmack API连接OpenFire的代码。当我试图在手机上运行应用程序时,我收到一条错误消息,说'不幸的是app已停止'。当我删除aSmack的代码时,该应用程序正常工作创建一个按钮。我还将jar添加到了类路径中。请帮帮我。
package com.example.demo;
import java.io.File;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
String username = "swapnil", password = "swapnil", host = "192.168.0.4", service = "mirana";
int port = 5222;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
final EditText et =(EditText)findViewById(R.id.editText1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// et.setText("Test");
XMPPConnection xmpp = null;
ConnectionConfiguration xmppConfig = new ConnectionConfiguration("192.168.0.4", 5222,"mirana");
xmppConfig.setSASLAuthenticationEnabled(true);
xmppConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
if (xmppConfig == null)
throw new NullPointerException("XMPPService must be configured before it can connect!");
try {
if (xmpp == null) {
xmpp = new XMPPConnection(xmppConfig);
}
xmpp.connect();
} catch (XMPPException ex) {
et.setText("ERROR !");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
以下是清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
没有日志,在看完你的代码之后,我可以猜到的是,你在主线程上运行了smack方法。例外应该是NetworkOnMainThreadException
。
您应该在按钮单击时在后台线程上执行任务。您可能想为此创建AsyncTask
。
此外,您没有Internet权限。所以请解决这个问题..
在实施Smack时,您需要负责维护Smack Connections及相关会话的会话。为此,您可以参考以下GitHub演示应用程序:Smack Android Demo
答案 1 :(得分:0)
您必须在 AndroidManifest.xml
中添加Internet
权限
<uses-permission android:name="android.permission.INTERNET" />
您需要在onCreate()
方法中添加以下代码。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);