Android Question App works when compiled in Debug(rapid) mode but not in release mode

Discussion in 'Android Questions' started by Rory Mapstone, May 7, 2015.

  1. Rory Mapstone

    Rory Mapstone Member Licensed User

    Good day,

    I have wrapped the Java smack library to be used in B4A. When I compile the app in Debug(rapid) mode I can establish and communicate with my xmpp server over the LAN. When I compile the exact same code using Release mode I am unable to connect to the server.

    I have also checked to make sure that internet and network permissions are present.

    Debug mode
    upload_2015-5-7_9-22-40.png

    Release mode
    upload_2015-5-7_9-22-54.png

    Java Lib:
    Code:
    package smack.b4a.lib;

    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.PacketListener;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.filter.MessageTypeFilter;
    import org.jivesoftware.smack.filter.PacketFilter;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Packet;
    import org.jivesoftware.smack.packet.Presence;
    import org.jivesoftware.smack.util.StringUtils;

    import anywheresoftware.b4a.BA;
    import anywheresoftware.b4a.BA.Events;
    import anywheresoftware.b4a.BA.Permissions;
    import anywheresoftware.b4a.BA.ShortName;
    import anywheresoftware.b4a.BA.Version;


    @Version(
    1.0f)
    @Permissions(values={
    "android.permission.INTERNET"})
    @ShortName("SmackLib")
    @Events(values={"receivedmessage (mMsg As String, , fromName As String)"})


    public class SmackLib {
        private ConnectionConfiguration connConfig;
        private XMPPConnection connection;
        private String mMsg="";
      
        public String autoConnect(String host, String port, String service, String u,String p)
        {
            ConnectionConfiguration _connConfig = new ConnectionConfiguration(host, Integer.parseInt(port), service);
            XMPPConnection _connection = new XMPPConnection(_connConfig);
            try {
                _connection.connect();
                _connection.login(u, p);
                Presence presence = new Presence(Presence.Type.available);
                _connection.sendPacket(presence);
            } catch (XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          
            return "login OK";
        }
      
        public String AndroidConnectionConfiguration(String host, String port, String service)
        {

            try
            {
                connConfig = new ConnectionConfiguration(host, Integer.parseInt(port), service);
            }
            catch(Exception e)
            {
                return e.getMessage();
            }
            return host;

        }
      
        public String MakeConnection()
        {
            try
            {
                connection = new XMPPConnection(connConfig);
            }
            catch (Exception e)
            {
                return "Error:" + e.getMessage();
            }
            return "Config set";

        }
      
        public String ConnectServer()
        {
            try
            {

                connection.connect();
            }
            catch(Exception e)
            {
                return "Error ConnectServer:" + e.getMessage();
            }

                return connection.getHost();
        }
      
        public String Disconnect()
        {
            try
            {

                connection.disconnect();
            }
            catch(Exception e)
            {
                return "Error:" + e.getMessage();
            }

                return "Disconnected";
        }
      
        public String loginUser(String u,String p)
        {

            try
            {

                connection.login(u, p);
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);

            }
            catch(Exception e)
            {
                return "Error:" + e.getMessage();
            }

            return "login OK";
        }
      
      
        public String sendMessage(String to, String text)
        {

            try
            {
          
                 Message msg = new Message(to, Message.Type.chat);
                 msg.setBody(text);
                 connection.sendPacket(msg);

            }
            catch(Exception e) {
                return "Error:" + e.getMessage();
            }
          
            return "send!";

        }
      
        public String ReceiveMessageEvent(final BA ba)
        {
            //mBA = new BA(null, null, null, mMsg, mMsg);
             PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
             connection.addPacketListener(new PacketListener()
             {
                 public void processPacket(Packet packet)
                 {
                     Message message = (Message) packet;
                     if (message.getBody() != null)
                     {
                         String fromName = StringUtils.parseBareAddress(message.getFrom());
                         mMsg = message.getBody();
                         //BA.raiseEvent2(this, false, "ReceivedMessage", true, new Object[] {mMsg});
                         ba.raiseEventFromDifferentThread(this, null, 0, "receivedmessage", false, new Object[] {mMsg, fromName});
                         //mBA.raiseEvent2(this, false, "receivedMessage", true, new Object[] {mMsg});
                         //BA.raiseEvent(this, "ReceivedMessage");
                     }
                 }
             }, filter);
             //mBA.raiseEventFromDifferentThread(this, null, 0, "receivedmessage", false, new Object[] {"Hi"});
            //mBA.raiseEvent2(this, false, "receivedmessage", true, new Object[] {"hI"});
             return "Listening";
        }
      
    }
    Any ideas?

    Regards
     
    Last edited: May 7, 2015
  2. Rory Mapstone

    Rory Mapstone Member Licensed User

    Compiling the app in debug(legacy) mode also does not work.

    Can someone tell me what the difference is?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Which error do you get?
     
  4. Rory Mapstone

    Rory Mapstone Member Licensed User

    Hi Erel,

    I do not get any compiling errors. The difference is, that the app can only make connection to my xmpp server when it is compiled in debug(rapid) mode.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    My guess:
    1. This library makes the network calls on the main thread (not good).
    2. It hides the error by printing it instead of throwing it (same). Check the unfiltered logs and you will see it.
    3. Solution: Workaround the NetworkOnMainThread exception
     
  6. Rory Mapstone

    Rory Mapstone Member Licensed User

    Thanks Erel,

    That solution worked.

    I am creating a service which runs new thread to make the connection so I was under the impression it would be safe.

    I think this could be the cause. RunOnGuiThread causes the thread to run on main thread.
    Code:
    If th.Running =False Then th.RunOnGuiThread("thread1",Params)
    I have a direction to focus on now, thanks again.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice