最简单的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmackAndroid.init(this);
// Create a connection to the jabber.org server.
XMPPTCPConnection conn1 = new XMPPTCPConnection("xmpp-hosting.de");
try {
conn1.connect();
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "Connected successfully!");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
但我在init()处得到NoClassDefFound异常。我注意到构建应用程序时有一些警告:
[2014-06-30 08:47:51 - AsmackTest] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(jnamed$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2014-06-30 08:47:51 - AsmackTest] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(jnamed$2) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2014-06-30 08:47:51 - AsmackTest] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(jnamed$3) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2014-06-30 08:47:52 - AsmackTest] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.xbill.DNS.UDPClient$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2014-06-30 08:47:53 - AsmackTest] Dx
trouble processing:
[2014-06-30 08:47:53 - AsmackTest] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/novell/sasl/client/DigestChallenge.class
...while processing com/novell/sasl/client/DigestChallenge.class
[2014-06-30 08:47:53 - AsmackTest] Dx
似乎合理的是,如果我可以摆脱警告代码将起作用。在日志中有一个建议从源构建类。很遗憾,我无法构建asmack代码:Cannot build asmack。所以我处于不确定状态。我在哪里开始排除故障?
Asmack版本:asmack-android-8-4.0.0.jar
堆栈跟踪:
06-30 15:34:33.609: E/AndroidRuntime(25444): FATAL EXCEPTION: main
06-30 15:34:33.609: E/AndroidRuntime(25444): Process: com.example.asmacktest, PID: 25444
06-30 15:34:33.609: E/AndroidRuntime(25444): java.lang.NoClassDefFoundError: org.jivesoftware.smack.SmackAndroid
06-30 15:34:33.609: E/AndroidRuntime(25444): at com.example.asmacktest.MainActivity.onCreate(MainActivity.java:31)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.Activity.performCreate(Activity.java:5248)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.os.Handler.dispatchMessage(Handler.java:102)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.os.Looper.loop(Looper.java:136)
06-30 15:34:33.609: E/AndroidRuntime(25444): at android.app.ActivityThread.main(ActivityThread.java:5102)
06-30 15:34:33.609: E/AndroidRuntime(25444): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 15:34:33.609: E/AndroidRuntime(25444): at java.lang.reflect.Method.invoke(Method.java:515)
06-30 15:34:33.609: E/AndroidRuntime(25444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-30 15:34:33.609: E/AndroidRuntime(25444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-30 15:34:33.609: E/AndroidRuntime(25444): at dalvik.system.NativeStart.main(Native Method)
06-30 15:34:43.501: E/WindowManager(1001): Starting window AppWindowToken{440206f0 token=Token{4373dbe0 ActivityRecord{4373da80 u0 com.example.asmacktest/.MainActivity t310}}} timed out
答案 0 :(得分:0)
https://github.com/Flowdalic/asmack/blob/master/README.asmack
如果使用ProGuard,则必须对其进行配置,以便不重要 Smack课程已经过优化:
# This dnsjava class uses old Sun API
-dontnote org.xbill.DNS.spi.DNSJavaNameServiceDescriptor
-dontwarn org.xbill.DNS.spi.DNSJavaNameServiceDescriptor
# See http://stackoverflow.com/questions/5701126, happens in dnsjava
-optimizations !code/allocation/variable
# Smack specific configuration
-keep class de.measite.smack.AndroidDebugger { *; }
-keep class * implements org.jivesoftware.smack.initializer.SmackInitializer
-keep class * implements org.jivesoftware.smack.provider.IQProvider
-keep class * implements org.jivesoftware.smack.provider.PacketExtensionProvider
-keep class * extends org.jivesoftware.smack.packet.Packet
-keep class org.jivesoftware.smack.ReconnectionManager
-keep class org.jivesoftware.smackx.disco.ServiceDiscoveryManager
-keep class org.jivesoftware.smackx.xhtmlim.XHTMLManager
-keep class org.jivesoftware.smackx.muc.MultiUserChat
-keep class org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager
-keep class org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager
-keep class org.jivesoftware.smackx.filetransfer.FileTransferManager
-keep class org.jivesoftware.smackx.iqlast.LastActivityManager
-keep class org.jivesoftware.smackx.commands.AdHocCommandManager
-keep class org.jivesoftware.smackx.ping.PingManager
-keep class org.jivesoftware.smackx.privacy.PrivacyListManager
-keep class org.jivesoftware.smackx.time.EntityTimeManager
-keep class org.jivesoftware.smackx.vcardtemp.VCardManager
-keep class org.jivesoftware.smack.CustomSmackConfiguration
答案 1 :(得分:0)
public static final String HOST = ""; //write your host name
public static final int PORT = 5222;
public static final String USERNAME = ""; //username and password with want to login
public static final String PASSWORD = "";
private XMPPConnection connection;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = XMPPChatDemoActivity.this;
recipient = (EditText) this.findViewById(R.id.toET);
textMessage = (EditText) this.findViewById(R.id.chatET);
listview = (ListView) this.findViewById(R.id.listMessages);
// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.sendBtn);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String to = ""; // username to whom you want to send message
String text = textMessage.getText().toString();
Log.i("XMPPChatDemoActivity", "Sending text " + text + " to "
+ to);
Message msg = new Message(to, Message.Type.chat);
if (connection != null) {
String Name = StringUtils.parseBareAddress(connection
.getUser());
Log.i("test", "Text send " + msg.getBody() + " from "
+ Name);
msg.setBody(text);
connection.sendPacket(msg);
}
}
});
connect();
}
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.e("XMPPChatDemoActivity", "Text Recieved "
+ message.getBody() + " from " + fromName);
mHandler.post(new Runnable() {
public void run() {
// Add the incoming message to the list view
}
});
}
}
}, filter);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (connection != null)
connection.disconnect();
} catch (Exception e) {
}
}
public void connect() {
final ProgressDialog dialog = ProgressDialog.show(this,
"Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(
HOST, PORT);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
Log.i("XMPPChatDemoActivity",
"Connected to " + connection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to connect to "
+ connection.getHost());
Log.e("XMPPChatDemoActivity", ex.toString());
setConnection(null);
}
try {
// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login(USERNAME, PASSWORD);
Log.i("XMPPChatDemoActivity",
"Logged in as " + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.d("XMPPChatDemoActivity",
"--------------------------------------");
Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.d("XMPPChatDemoActivity",
"User: " + entry.getUser());
Log.d("XMPPChatDemoActivity",
"Name: " + entry.getName());
Log.d("XMPPChatDemoActivity",
"Status: " + entry.getStatus());
Log.d("XMPPChatDemoActivity",
"Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry
.getUser());
Log.d("XMPPChatDemoActivity", "Presence Status: "
+ entryPresence.getStatus());
Log.d("XMPPChatDemoActivity", "Presence Type: "
+ entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.d("XMPPChatDemoActivity", "Presence : "
+ entryPresence);
}
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to log in as "
+ USERNAME);
Log.e("XMPPChatDemoActivity", ex.toString());
setConnection(null);
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}