4
votes

Hello Everyone i am new to android and i am currently stuck on this. I have to return list of public rooms created on xmpp server. The problem i am having is that the code below works fine for java but there is a null pointer exception in case of android. Any help regarding this would be appreciated.

I am using an openfire server and testing it on local machine so that is the reason why i am using ip Address instead of domain name.

I am using smack library for JAVA and Asmack Library for android

String server_name = "192.168.3.113";
    ConnectionConfiguration config = new ConnectionConfiguration(
            server_name, 5222);
    XMPPConnection connection = new XMPPConnection(config);
    try {
        connection.connect();

        connection.login("s1", "123");

        Collection<HostedRoom> rooms = MultiUserChat.getHostedRooms(
                connection, "conference.geekoid");
        for (HostedRoom room : rooms) {
            System.out.println(room.getName());
        }

    } catch (XMPPException e) {
        System.out.println("Error" + e.getMessage() + "\n"); //for JAVA
                    log.e("Android Error",e.getmessage());  // For Android
    }
2
The stacktrace of the NPE would be helpful. We can't help if you don't show use the actual error. Also, make sure to use an up to date version of aSmack. See this questionFlow
Thankyou very much for ur help. it was a library issue. So i have already solved itSheraz Ahmad Khilji
If you have solved your question, add the solution as an answer: meta.stackexchange.com/questions/17845/…MattJ

2 Answers

6
votes

The problem is that the static block of the ServiceDiscoveryManager class has to be evaluated before any connection is created. In smack this is done via an configuration file, but this approach does not work on Android and therefore on aSmack.

The workaround mentioned in the answer is somehow ugly, since you really don't want to use the Constructor to fetch the SDM object, instead the get() method should be used. But the get() method only works if there was actually a SDM created for the connection.

So in order to init the SDM correctly on Android you need to call the full forName notation to init the static blocks of the class before you create the first (XMPP)Connection object.

Class.forName("org.jivesoftware.smackx.ServiceDiscoveryManager", true, ClassLoader.getSystemClassLoader()):

This is tracked as aSmack Issue 8

6
votes

I have found the Solution to the problem.

The Android asmack library was using this in getHostedRooms(Connection connection, String serviceName) method

ServiceDiscoveryManager discoManager =ServiceDiscoveryManager.getInstanceFor(connection);

i replaced it with

ServiceDiscoveryManager discoManager = new ServiceDiscoveryManager(connection);

For those who are confused where this method is its in

Package: org.jivesoftware.smackx.muc

File: MultiUserChat.java

After you have done this. We have to register all the providers in Android whose detail can be found here. These providers are automatically registered when are using JAVA's smack library (In java Development) but in Android we have to register them ourself.