Remote-server-error(502) when there is no network connection

828 views Asked by At

This is how I establish the connection between my XMPP client and server and then log in with a background service:

public class MessageService extends Service {
private String TAG = "MessageService";
private XMPPConnection connection;
private final IBinder mBinder = new MyBinder();

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mBinder;
}

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "started");
    new Connect().execute("");
    return START_STICKY;
}

private class Connect extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(
                SettingsDM.IP_ADDRESS, SettingsDM.PORT);

        XMPPConnection connection = new XMPPConnection(
                connectionConfiguration);
        Log.i(TAG, "getting ready to connect...");
        try {
            connection.connect();
            Log.i(TAG, "Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            Log.e(TAG, "Failed to connect to " + connection.getHost());
            Log.e(TAG, ex.toString());
            setConnection(null);
        }
        try {
            connection.login(SettingsDM.TEST_USERNAME,
                    SettingsDM.TEST_PASSWORD);
            Log.i(TAG, "Logged in as " + connection.getUser());


            //setConnection(connection);
        } catch (XMPPException ex) {
            Log.e(TAG, "Failed to log in as " + SettingsDM.TEST_USERNAME);
            Log.e(TAG, ex.toString());
            setConnection(null);
        }
        return null;
    }
}
}

This works okay when there is a network connection. When there's no network connection however, I'd expect for the exception to be caught in catch(XMPPException e). But it doesn't get caught and the app crashes. The background service too crashes and attempts to restart a couple of times but keeps crashing.

Below is the error log:

11-10 16:14:20.909: E/MessageService(32759): Failed to connect to 192.168.1.4
11-10 16:14:20.910: E/MessageService(32759): XMPPError connecting to 192.168.1.4:5222.: remote-server-error(502) XMPPError connecting to 192.168.1.4:5222.
11-10 16:14:20.910: E/MessageService(32759):   -- caused by: java.net.ConnectException: failed to connect to /192.168.1.4 (port 5222): connect failed: ENETUNREACH (Network is unreachable)
11-10 16:14:20.956: E/AndroidRuntime(32759): FATAL EXCEPTION: AsyncTask #1
11-10 16:14:20.956: E/AndroidRuntime(32759): java.lang.RuntimeException: An error occured while executing doInBackground()
11-10 16:14:20.956: E/AndroidRuntime(32759):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.lang.Thread.run(Thread.java:856)
11-10 16:14:20.956: E/AndroidRuntime(32759): Caused by: java.lang.IllegalStateException: Not connected to server.
11-10 16:14:20.956: E/AndroidRuntime(32759):    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:217)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at org.jivesoftware.smack.Connection.login(Connection.java:353)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at com.niilaryea.android.service.MessageService$Connect.doInBackground(MessageService.java:77)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at com.niilaryea.android.service.MessageService$Connect.doInBackground(MessageService.java:1)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-10 16:14:20.956: E/AndroidRuntime(32759):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-10 16:14:20.956: E/AndroidRuntime(32759):    ... 5 more
11-10 16:14:21.969: E/GraphicBufferAllocator(32759): FATAL: can't find the mmumapper module

How do I prevent the background service and the app from crashing when there's no network connection available, but to rather keep checking until there's an active network connection available?

1

There are 1 answers

1
Eugene Kuzmenko On BEST ANSWER

You have java.net.ConnectException , so you should use Exception instead of XMPPException