Skip navigation
18846 Views 13 Replies Latest reply: Jun 23, 2011 12:16 AM by duncancarroll RSS
Bronze 12 posts since
Feb 11, 2005
Currently Being Moderated

Jan 18, 2008 1:16 PM

Shot in the dark - Smack API, Android SDK and ClassCastExceptions.

 

I think this is a bit of a long shot but I figured I might as well give this a try.

 

 

 

 

 

So I'm using this patched version of the Smack API with the Google Phone Android SDK to try to get a custom client working on that platform.  What I keep running into in different places are ClassCastExceptions when the API tries to cast Packets into subclasses.

 

 

Here's an example from the ServiceDiscoveryManager

 

 

         PacketCollector collector =

            connection.createPacketCollector(new PacketIDFilter(disco.getPacketID()));

 

        connection.sendPacket(disco);

 

        // Wait up to 5 seconds for a result.

        IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

        // Stop queuing results

        collector.cancel();

        if (result == null) {

            Log.i("TestClient", "++++ No response from the server.");

        }

        if (result.getType() == IQ.Type.ERROR) {

            Log.i("TestClient",result.getError().toString());

        }

        Log.i("TestClient", ">>>> \"" + result.getClass().getName()+"\"");

        Packet test = (Packet) result;

        Log.i("TestClient", "Result class loader: " + test.getClass().getClassLoader().toString());

        Log.i("TestClient", "DI Class loader: " + DiscoverItems.class.getClassLoader().toString());

 

        return (DiscoverItems) test;

 

 

 

 

 

the cast of the Packet to DiscoverItems will fail with a ClassCastException.   As you can see from the Log statements  in this case I've even made sure the Class loaders are the same and that the Class is the parent class. I can even successfully cast it to a Packet but not to a sub class.

 

 

Any help would be appreciated before I pull my hair out.

 

 

 

 

 

Thanks.

 

 

    • Gaston Dombiak Jiver 3,863 posts since
      Sep 26, 2001

      Hey Mike,

       

      That usually happens when smackx.jar is missing in the classpath. When this problem happens with custom classes (i.e. classes that you implemented) then it could be a problem of registering your extension as a PacketExtension or IQExtension.

       

      Regards,

       

        -- Gato

  • gperrot Bronze 11 posts since
    Jul 1, 2008

     

    Hello, i have a problem with the vCard.load function that does nothing in an Android application but works well with a J2SE project.

     

     

    To fix that problem, I tried to add the following line before creating the connection : ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", new VCardProvider());

     

     

    Since i added this, i have my listener function connectionClosedOnError (from ConnectionListener) which is called just after the first call to vCard.load...

     

     

    I tried with the standard smack jars and also the ones provided by the bhoost project: same result with and without the code line above.

     

     

    • Vincent BARAT Bronze 1 posts since
      Jul 2, 2008

      I faced the same issue and found a fix.

       

      Actually, on Android, smack don't take into account everything that is part of the smack.jar and smackx.jar meta data (basically, the content of the files "smack-config.xml and smack.provider is ignored) which explain why the various packet providers must be initialized by hand. I guess that the META-INF section is discarded when Android convert the .jar inot its private format.

       

      Then, regarding your precise problem, I manage to fix this by  applying the following patch:

       

      
      
      Index: source/org/jivesoftware/smackx/provider/VCardProvider.java
      ===================================================================
      --- source/org/jivesoftware/smackx/provider/VCardProvider.java  (revision 10629)
      +++ source/org/jivesoftware/smackx/provider/VCardProvider.java  (working copy)
      @@ -244,14 +244,16 @@
      }
      
      private void appendText(StringBuilder result, Node node) {
      -            NodeList childNodes = node.getChildNodes();
      -            for (int i = 0; i < childNodes.getLength(); i++) {
      -                Node nd = childNodes.item(i);
      -                String nodeValue = nd.getNodeValue();
      -                if (nodeValue != null) {
      -                    result.append(nodeValue);
      +            if (node.hasChildNodes()) {
      +                NodeList childNodes = node.getChildNodes();
      +                for (int i = 0; i < childNodes.getLength(); i++) {
      +                    Node nd = childNodes.item(i);
      +                    String nodeValue = nd.getNodeValue();
      +                    if (nodeValue != null) {
      +                        result.append(nodeValue);
      +                    }
      +                    appendText(result, nd);
      }
      -                appendText(result, nd);
      }
      }
      }
      
      

       

      I guess there is an error in the function that is visible only on Android because the org.w3c.dom package is not exactely the same then the one present in smack.jar but I'm not sure, since I'm not a Java expert. So, if somebody has information about this, he is welcome!

       

      Cheers,

More Like This

  • Retrieving data ...

Bookmarked By (1)