我正在使用http://asmack.freakempire.de/4.0.5/中的asmack-android-8-4.0.5.jar进行连接并成功登录,但是当我尝试加载自己的vcard时,它将在vcard.doload方法中抛出classcastexception。请帮助任何人。以下是我正在使用的完整代码。

private class ConnectTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {

        Roster.setDefaultSubscriptionMode(SubscriptionMode.accept_all);
        configure(new ProviderManager());
        ConnectionConfiguration config = new ConnectionConfiguration(
                "my host here", 5222, "xmpp.jp");
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        config.setCompressionEnabled(true);
        ProviderManager.addIQProvider("vCard", "vcard-temp",
                new VCardProvider());
        connection = new XMPPTCPConnection(config);
        try {
            connection.connect();
            System.out.println("sanidhya09 connected");

            try {
                connection.login("sanidhya09", "*****");
                System.out.println("sanidhya09 logged in ");
            } catch (SaslException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            System.err.println("sanidhya09 Failed to connect to "
                    + "xmpp.jp");
            return null;
        }
        return null;
    }

    protected void onPostExecute(Long result) {
        Presence pres = new Presence(Presence.Type.available);

        try {
            connection.sendPacket(pres);
        } catch (NotConnectedException e1) {
            e1.printStackTrace();
        }
        VCard vCard = new VCard();
        try {
            ProviderManager.addIQProvider("vCard", "vcard-temp",
                    new VCardProvider());
            System.out.println("sanidhya09 vcard trying");
            vCard.load(connection);
            System.out.println("sanidhya09 vcard loaded");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


这是例外:

  java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2 cannot be cast to org.jivesoftware.smackx.vcardtemp.packet.VCard
11-05 16:09:57.379: W/System.err(26675):    at org.jivesoftware.smackx.vcardtemp.packet.VCard.doLoad(VCard.java:560)
11-05 16:09:57.379: W/System.err(26675):    at org.jivesoftware.smackx.vcardtemp.packet.VCard.load(VCard.java:542)
11-05 16:09:57.379: W/System.err(26675):    at com.sanidhya.chatdemo.MainActivity$ConnectTask.onPostExecute(MainActivity.java:111)
11-05 16:09:57.379: W/System.err(26675):    at com.sanidhya.chatdemo.MainActivity$ConnectTask.onPostExecute(MainActivity.java:1)
11-05 16:09:57.380: W/System.err(26675):    at android.os.AsyncTask.finish(AsyncTask.java:632)
11-05 16:09:57.380: W/System.err(26675):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-05 16:09:57.380: W/System.err(26675):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-05 16:09:57.380: W/System.err(26675):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 16:09:57.380: W/System.err(26675):    at android.os.Looper.loop(Looper.java:136)
11-05 16:09:57.380: W/System.err(26675):    at android.app.ActivityThread.main(ActivityThread.java:5086)
11-05 16:09:57.380: W/System.err(26675):    at java.lang.reflect.Method.invokeNative(Native Method)
11-05 16:09:57.380: W/System.err(26675):    at java.lang.reflect.Method.invoke(Method.java:515)
11-05 16:09:57.380: W/System.err(26675):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-05 16:09:57.380: W/System.err(26675):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-05 16:09:57.380: W/System.err(26675):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

我现在开始工作了。实际上,它们是xml响应中的问题。它没有化身或化身有故障。保存头像时,我什至输入了错误的base64。

通过这样保存化身使其正常工作:

    private void uploadProfilePic() {
    try {
        Bitmap resized = Bitmap.createScaledBitmap(selected_img,128, 128,
                true);
        obj_UserInfo.setProfilePic(resized);
        String get64BaseString = get64BaseString(resized);

        VCard vCard = new VCard();

        vCard.setAvatar(get64BaseString, "image/jpeg");
        vCard.save(CGlobalVarriables.connection);
        System.out.println("image upload successfuls");
        CGlobalVarriables.obj_UserInfo.setProfilePic(resized);


    } catch (Exception e) {
        e.printStackTrace();
    }
}


通过以下方式加载了Vcard:

private Bitmap getProfilePic() {

    VCard vCard = new VCard();
    try {
        vCard.load(CGlobalVarriables.connection);
        byte[] array = vCard.getAvatar();
        Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0,
                array.length);

        return bitmap;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

关于android - 调用vcard.load(conn)时的Android asmack vcard classcastException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26752285/

10-10 19:48