基本上,下载了OpenFire Server和Spark Client。 推出了这两款产品,并设法让两个火花用户通过2台笔记本电脑互相聊天。 项目到目前为止是成功的,然后我尝试在eclipse上创建一个应用程序 http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html本教程(我也按照前两部分) 我试过打击和asmack。
头等舱(XmppManager):
package montytest.xmppproject;
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.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
public class XmppManager {
private static final int packetReplyTimeout = 500; // millis
private String server;
private int port;
private ConnectionConfiguration config;
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
public XmppManager(String server, int port) {
this.server = server;
this.port = port;
}
public void init() throws XMPPException {
System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
config = new ConnectionConfiguration(server, port);
config.setSASLAuthenticationEnabled(false);
config.setSecurityMode(SecurityMode.disabled);
connection = new XMPPConnection(config);
connection.connect();
System.out.println("Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
messageListener = new MyMessageListener();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection!=null && connection.isConnected()) {
connection.login(username, password);
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available? Type.available: Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection!=null && connection.isConnected()) {
connection.disconnect();
}
}
public void printRoster() throws Exception {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println(String.format("Buddy:%1$s - Status:%2$s",
entry.getName(), entry.getStatus()));
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
public void createEntry(String user, String name) throws Exception {
System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
class MyMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
}
}
}
第2类(XmppTest):
package montytest.xmppproject;
public class XmppTest {
public static void main(String[] args) throws Exception {
String username = "testuser1";
String password = "zaq123";
XmppManager xmppManager = new XmppManager("jawad", 5222);
xmppManager.init();
xmppManager.performLogin(username, password);
xmppManager.setStatus(true, "Hello everyone");
String buddyJID = "testuser2";
String buddyName = "testuser2";
xmppManager.createEntry(buddyJID, buddyName);
xmppManager.sendMessage("Hello mate", "testuser2@jawad");
xmppManager.printRoster();
boolean isRunning = true;
while (isRunning) {
Thread.sleep(50);
}
xmppManager.destroy();
}
}
第3课(开始):
package montytest.xmppproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Start extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void launchForm(View button) {
Intent launchFormActivity = new Intent(this, XmppManager.class);
startActivity(launchFormActivity);
}
}
这个类基本上是一个带有通向第一个类的按钮的页面
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="montytest.xmppproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Start"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".XmppManager"
android:label="@string/manager" >
</activity>
<activity
android:name=".Xmpptest"
android:label="@string/test" >
</activity>
</application>
</manifest>
并最后在模拟器上运行后使用Logcat:
10-30 11:48:26.717: D/gralloc_goldfish(738): Emulator without GPU emulation detected.
10-30 11:50:28.027: D/AndroidRuntime(738): Shutting down VM
10-30 11:50:28.027: W/dalvikvm(738): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-30 11:50:28.067: E/AndroidRuntime(738): FATAL EXCEPTION: main
10-30 11:50:28.067: E/AndroidRuntime(738): java.lang.IllegalStateException: Could not find a method Start(View) in the activity class montytest.xmppproject.Start for onClick handler on view class android.widget.Button with id 'Button_start'
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$1.onClick(View.java:3578)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View.performClick(View.java:4084)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$PerformClick.run(View.java:16966)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Handler.handleCallback(Handler.java:615)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Looper.loop(Looper.java:137)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 11:50:28.067: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-30 11:50:28.067: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 11:50:28.067: E/AndroidRuntime(738): at dalvik.system.NativeStart.main(Native Method)
10-30 11:50:28.067: E/AndroidRuntime(738): Caused by: java.lang.NoSuchMethodException: Start [class android.view.View]
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.Class.getMethod(Class.java:915)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$1.onClick(View.java:3571)
10-30 11:50:28.067: E/AndroidRuntime(738): ... 11 more
10-30 11:48:26.717: D/gralloc_goldfish(738): Emulator without GPU emulation detected.
10-30 11:50:28.027: D/AndroidRuntime(738): Shutting down VM
10-30 11:50:28.027: W/dalvikvm(738): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-30 11:50:28.067: E/AndroidRuntime(738): FATAL EXCEPTION: main
10-30 11:50:28.067: E/AndroidRuntime(738): java.lang.IllegalStateException: Could not find a method Start(View) in the activity class montytest.xmppproject.Start for onClick handler on view class android.widget.Button with id 'Button_start'
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$1.onClick(View.java:3578)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View.performClick(View.java:4084)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$PerformClick.run(View.java:16966)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Handler.handleCallback(Handler.java:615)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.os.Looper.loop(Looper.java:137)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 11:50:28.067: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-30 11:50:28.067: E/AndroidRuntime(738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 11:50:28.067: E/AndroidRuntime(738): at dalvik.system.NativeStart.main(Native Method)
10-30 11:50:28.067: E/AndroidRuntime(738): Caused by: java.lang.NoSuchMethodException: Start [class android.view.View]
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-30 11:50:28.067: E/AndroidRuntime(738): at java.lang.Class.getMethod(Class.java:915)
10-30 11:50:28.067: E/AndroidRuntime(738): at android.view.View$1.onClick(View.java:3571)
10-30 11:50:28.067: E/AndroidRuntime(738): ... 11 more
有什么方法可以解决这个问题吗?