SImple Asmack program not working

2019-05-31 01:33发布

Simplest possible code:

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();
        }
    }

But I get a NoClassDefFound Exception at init(). I noticed there are some warnings while building the app:

[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

Seems reasonable that if I can get rid of warnings the code will work. In the log there is a suggestion to build the class from source. Unfortunately I am unable to build the asmack code: Cannot build asmack. So I am in limbo ..Where do I begun to troubleshoot?

Asmack version: asmack-android-8-4.0.0.jar

Stacktrace:

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

2条回答
啃猪蹄的小仙女
2楼-- · 2019-05-31 01:53
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();
        }
查看更多
冷血范
3楼-- · 2019-05-31 02:04

https://github.com/Flowdalic/asmack/blob/master/README.asmack

If you use ProGuard, you have to configure it so that no important Smack classes are optimized away:

# 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
查看更多
登录 后发表回答