Loading config file in android

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

cslwkah
Posts: 13
Joined: 21 Feb 2011, 10:40

Loading config file in android

Postby cslwkah » 02 Mar 2011, 15:15

I try to test the api in android but don't know how to load the config file. I put the sfs-config.xml under asset folder then call

Code: Select all

sfs.loadConfig("file:///android_asset/sfs-config.xml", true);


Get an exception:

Code: Select all

03-02 16:42:57.575: WARN/System.err(248): java.io.IOException: Couldn't open file:/file:/android_asset/sfs-config.xml


After several tries of different path, I give up. Instead, I use the connect() method and input the ip/port like this:

Code: Select all

sfs.connect("127.0.0.1", 9933);


This time no exception found but also no result comes back. None of the event listeners are called.

Any one have idea?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 02 Mar 2011, 17:50

I have not tested the API under Android - I dont have a test device yet.

On the connect part - if you actually specified 127.0.0.1 on the code you deploy to the phone, it would expect the server to run on your localhost. I'd suggest putting in the servers real IP for one :-)

There is also a known bug in the API at the moment not reporting an error when it cannot connect to a server. So you are most likely also hit by that

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
cslwkah
Posts: 13
Joined: 21 Feb 2011, 10:40

Postby cslwkah » 03 Mar 2011, 03:48

Still can not connect.... Please help :cry:

I change the ip to 192.168.0.106 in connect() method, and nothing come back at first. Then I see the log in sfs2x change.

Code: Select all

03 3月 2011 13:15:30,888 INFO  [main] smartfoxserver.v2.SmartFoxServer     - SmartFoxServer 2X (2.0.0-RC1b) READY!
03 3月 2011 13:15:57,706 INFO  [SocketReader] bitswarm.core.SocketAcceptor     - Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.0.106:62463 } on Server port: 9932 <---> 62463
03 3月 2011 13:15:58,296 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] v2.controllers.SystemController     - {IN}: Handshake



After 1-2 seconds, an event come back saying it is "Diaconnected!" and server side generate this log.

Code: Select all

03 3月 2011 13:16:20,944 INFO  [Scheduler1-thread-1] bitswarm.sessions.DefaultSessionManager     - Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.0.106:62463 }
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 03 Mar 2011, 06:39

So basic connection works - good. Else it would not send a handshake.

Need to ask the standard questions though for helping on further.

* I would need the client side log at a "INFO" to see what comes in/out. Set up file appender and dump there if you cant see the console output and paste it

* Whats the client code you run?

* Does your client code run as standalone Java?

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
cslwkah
Posts: 13
Joined: 21 Feb 2011, 10:40

Postby cslwkah » 04 Mar 2011, 03:19

My client side is run on android emulator. it is the logcat result in android.

Code: Select all

03-04 03:08:09.303: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.test.android.multiplayer/.BasicClientExample bnds=[83,138][157,217] }
03-04 03:08:09.513: INFO/ActivityManager(52): Start proc com.test.android.multiplayer for activity com.test.android.multiplayer/.BasicClientExample: pid=252 uid=10029 gids={3003, 1015}
03-04 03:08:10.083: INFO/System.out(252): initSmartFox...
03-04 03:08:10.133: INFO/dalvikvm(252): DexOpt: access denied from Lorg/slf4j/LoggerFactory; to field Lorg/slf4j/impl/StaticLoggerBinder;.SINGLETON
03-04 03:08:10.133: WARN/dalvikvm(252): VFY: unable to resolve static field 3179 (SINGLETON) in Lorg/slf4j/impl/StaticLoggerBinder;
03-04 03:08:10.143: INFO/dalvikvm(252): DexOpt: access denied from Lorg/slf4j/LoggerFactory; to field Lorg/slf4j/impl/StaticLoggerBinder;.SINGLETON
03-04 03:08:10.143: WARN/dalvikvm(252): VFY: unable to resolve static field 3179 (SINGLETON) in Lorg/slf4j/impl/StaticLoggerBinder;
03-04 03:08:10.433: INFO/System.out(252): initSmartFox Done
03-04 03:08:10.513: WARN/System.err(252): log4j:WARN No appenders could be found for logger (sfs2x.client.core.EventDispatcher).
03-04 03:08:10.513: WARN/System.err(252): log4j:WARN Please initialize the log4j system properly.
03-04 03:08:10.603: ERROR/dalvikvm(252): Could not find class 'net.sf.json.JSONObject', referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.decodeJsonObject
03-04 03:08:10.623: WARN/dalvikvm(252): VFY: unable to resolve instanceof 545 (Lnet/sf/json/JSONObject;) in Lcom/smartfoxserver/v2/protocol/serialization/DefaultSFSDataSerializer;
03-04 03:08:10.632: WARN/dalvikvm(252): VFY: unable to find class referenced in signature (Lnet/sf/json/JSONArray;)
03-04 03:08:10.643: INFO/dalvikvm(252): Could not find method net.sf.json.JSONArray.iterator, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.decodeSFSArray
03-04 03:08:10.653: WARN/dalvikvm(252): VFY: unable to resolve virtual method 1929: Lnet/sf/json/JSONArray;.iterator ()Ljava/util/Iterator;
03-04 03:08:10.663: WARN/dalvikvm(252): VFY: unable to find class referenced in signature (Lnet/sf/json/JSONObject;)
03-04 03:08:10.663: INFO/dalvikvm(252): Could not find method net.sf.json.JSONObject.keySet, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.decodeSFSObject
03-04 03:08:10.673: WARN/dalvikvm(252): VFY: unable to resolve virtual method 1934: Lnet/sf/json/JSONObject;.keySet ()Ljava/util/Set;
03-04 03:08:10.693: INFO/dalvikvm(252): Could not find method org.apache.commons.io.IOUtils.closeQuietly, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.getBlobData
03-04 03:08:10.693: WARN/dalvikvm(252): VFY: unable to resolve static method 1936: Lorg/apache/commons/io/IOUtils;.closeQuietly (Ljava/io/InputStream;)V
03-04 03:08:10.703: INFO/dalvikvm(252): Could not find method org.apache.commons.io.IOUtils.closeQuietly, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.getBlobData
03-04 03:08:10.703: WARN/dalvikvm(252): VFY: unable to resolve static method 1936: Lorg/apache/commons/io/IOUtils;.closeQuietly (Ljava/io/InputStream;)V
03-04 03:08:10.713: INFO/dalvikvm(252): Could not find method org.apache.commons.io.IOUtils.closeQuietly, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.getBlobData
03-04 03:08:10.713: WARN/dalvikvm(252): VFY: unable to resolve static method 1936: Lorg/apache/commons/io/IOUtils;.closeQuietly (Ljava/io/InputStream;)V
03-04 03:08:10.713: INFO/dalvikvm(252): Could not find method org.apache.commons.lang.StringUtils.capitalize, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.readValueFromGetter
03-04 03:08:10.723: WARN/dalvikvm(252): VFY: unable to resolve static method 1937: Lorg/apache/commons/lang/StringUtils;.capitalize (Ljava/lang/String;)Ljava/lang/String;
03-04 03:08:10.733: INFO/dalvikvm(252): Could not find method org.apache.commons.lang.StringUtils.capitalize, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.readValueFromGetter
03-04 03:08:10.743: WARN/dalvikvm(252): VFY: unable to resolve static method 1937: Lorg/apache/commons/lang/StringUtils;.capitalize (Ljava/lang/String;)Ljava/lang/String;
03-04 03:08:10.753: ERROR/dalvikvm(252): Could not find class 'java.util.ArrayDeque', referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.setObjectField
03-04 03:08:10.773: WARN/dalvikvm(252): VFY: unable to resolve new-instance 306 (Ljava/util/ArrayDeque;) in Lcom/smartfoxserver/v2/protocol/serialization/DefaultSFSDataSerializer;
03-04 03:08:10.773: ERROR/dalvikvm(252): Could not find class 'java.util.concurrent.LinkedBlockingDeque', referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.setObjectField
03-04 03:08:10.773: WARN/dalvikvm(252): VFY: unable to resolve new-instance 361 (Ljava/util/concurrent/LinkedBlockingDeque;) in Lcom/smartfoxserver/v2/protocol/serialization/DefaultSFSDataSerializer;
03-04 03:08:10.793: INFO/dalvikvm(252): Could not find method org.apache.commons.lang.StringUtils.capitalize, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.writeValueFromSetter
03-04 03:08:10.793: WARN/dalvikvm(252): VFY: unable to resolve static method 1937: Lorg/apache/commons/lang/StringUtils;.capitalize (Ljava/lang/String;)Ljava/lang/String;
03-04 03:08:10.803: INFO/dalvikvm(252): Could not find method net.sf.json.JSONArray.fromObject, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.array2json
03-04 03:08:10.803: WARN/dalvikvm(252): VFY: unable to resolve static method 1928: Lnet/sf/json/JSONArray;.fromObject (Ljava/lang/Object;)Lnet/sf/json/JSONArray;
03-04 03:08:10.813: INFO/dalvikvm(252): Could not find method net.sf.json.JSONArray.fromObject, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.json2array
03-04 03:08:10.834: WARN/dalvikvm(252): VFY: unable to resolve static method 1928: Lnet/sf/json/JSONArray;.fromObject (Ljava/lang/Object;)Lnet/sf/json/JSONArray;
03-04 03:08:10.834: INFO/dalvikvm(252): Could not find method net.sf.json.JSONObject.fromObject, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.json2object
03-04 03:08:10.834: WARN/dalvikvm(252): VFY: unable to resolve static method 1931: Lnet/sf/json/JSONObject;.fromObject (Ljava/lang/Object;)Lnet/sf/json/JSONObject;
03-04 03:08:10.834: INFO/dalvikvm(252): Could not find method net.sf.json.JSONObject.fromObject, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2json
03-04 03:08:10.834: WARN/dalvikvm(252): VFY: unable to resolve static method 1931: Lnet/sf/json/JSONObject;.fromObject (Ljava/lang/Object;)Lnet/sf/json/JSONObject;
03-04 03:08:10.843: INFO/dalvikvm(252): Could not find method java.sql.ResultSet.getNString, referenced from method com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.resultSet2object
03-04 03:08:10.853: WARN/dalvikvm(252): VFY: unable to resolve interface method 1265: Ljava/sql/ResultSet;.getNString (I)Ljava/lang/String;
03-04 03:08:11.733: INFO/ActivityManager(52): Displayed activity com.test.android.multiplayer/.BasicClientExample: 2292 ms (total 2292 ms)
03-04 03:08:38.462: INFO/System.out(252): Event come back!!
03-04 03:08:38.472: INFO/System.out(252): Disconnected!


It is source code run in client side. I just change the pure java code to android. I also add some little stuff, a webview and a button to check if network is ON.

Code: Select all

package com.test.android.multiplayer;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import com.smartfoxserver.v2.exceptions.SFSException;

import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.requests.JoinRoomRequest;
import sfs2x.client.requests.LoginRequest;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class BasicClientExample extends Activity implements IEventListener{
   
   WebView web;
   
   
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
        initSmartFox();
         
        setContentView(R.layout.main);
       
        Button ipBtn = (Button)findViewById(R.id.ipBtn);
        ipBtn.setOnClickListener(new OnClickListener(){
         @Override
         public void onClick(View arg0) {
            Dialog d = new Dialog(BasicClientExample.this);
            d.setTitle("IP = " + getLocalIpAddress());
            d.setCancelable(true);
            d.show();
         }
           
        });
        web = new WebView(this);
        WebViewClient client = new WebViewClient();
        web.setWebViewClient(client);
        ((ViewGroup)findViewById(R.id.webViewContainer)).addView(web);
        web.loadUrl("http://google.com");
       
        Button connectBtn = (Button)findViewById(R.id.connectBtn);
        connectBtn.setOnClickListener(new OnClickListener(){
         @Override
         public void onClick(View arg0) {
            String url = ((EditText)findViewById(R.id.urlText)).getText().toString();
            web.loadUrl(url);
         }
           
        });
       
    }
   
    @Override
    public void onDestroy(){
       if(sfs!=null){
          sfs.removeAllEventListeners();
          sfs.disconnect();
       }
       sfs = null;
       super.onDestroy();
    }
   
    public String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            System.out.println("Error:"+ex.getMessage());
        }
        return null;
    }
   
    /**
     * Multiplayer part
     */
    private SmartFox sfs = null;
   
    private void initSmartFox(){
       System.out.println("initSmartFox...");
       
       sfs = new SmartFox();
       sfs.setDebug(true);
       
       //sfs.loadConfig("//android_asset/config/sfs-config.xml", true);
       
      
      sfs.addEventListener(SFSEvent.CONNECTION, this);
      sfs.addEventListener(SFSEvent.LOGIN, this);
      sfs.addEventListener(SFSEvent.ROOM_JOIN, this);
      sfs.addEventListener(SFSEvent.CONNECTION_LOST, this);
      
      sfs.connect("192.168.0.106", 9932);
      //sfs.loadConfig("file:///android_asset/config/sfs-config.xml", true);
      //sfs.loadConfig("file:///android_asset/sfs-config.xml", true);
      
      System.out.println("initSmartFox Done");
    }

    @Override
   public void dispatch(BaseEvent event) throws SFSException
   {
       System.out.println("Event come back!!");
       
       if (event.getType().equals(SFSEvent.CONNECTION))
       {
          System.out.println("Connected. Now sending login request.");
          
          // Login as guest in current zone
          sfs.send(new LoginRequest("", "", sfs.getCurrentZone()));
       }
      
       else if (event.getType().equals(SFSEvent.CONNECTION_LOST))
       {
          System.out.println("Disconnected!");
       }
      
       else if (event.getType().equals(SFSEvent.LOGIN))
       {
          String roomName = "The Lobby";
          System.out.println("Logged in. Now joining '" + roomName + "' room");
          sfs.send(new JoinRoomRequest(roomName));
       }
      
       else if (event.getType().equals(SFSEvent.ROOM_JOIN))
       {
          System.out.println("Room joined: " + event.getArguments().get("room"));
       }
      
      
   }
}


My server is setup on 192.168.0.106, opening port 9932. I think the server side setting should be correct because the pure java code works perfectly using the same server. It can make a connection and join room.

But when I move the code to android, it fail. It get a handshake, then wait for 1-2 seconds, it is disconnected :cry:
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 04 Mar 2011, 10:10

Lots of lovely warnings and even errors in the log. So no wonder that you do not get it working.

But it seems to be some library related problems.

"Could not find class 'net.sf.json.JSONObject'" for example - are you including all needed jar files in your project?

and "Could not find class 'java.util.ArrayDeque'"

Not knowing much about Android development, it looks to me like the JDK used in Android is limited / not supporting full java? Is that correctly understood?

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
cslwkah
Posts: 13
Joined: 21 Feb 2011, 10:40

Postby cslwkah » 04 Mar 2011, 12:02

Yes. All jar in the zip file are put into my project.

I think, not all default java class are supported in android.
Please check here to see which class are supported.
http://developer.android.com/reference/packages.html


for example, net.sf.json.JSONObject is required in your library, but normally in android we use org.json.JSONObject instead. So I doubt if android include this library.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 04 Mar 2011, 12:14

The file spewing out the errors (default sfs serializer class) is one that I didnt code - a core client jar file from the server guys. So need to await what they can do.

Will take a chat with them early next week.

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
User avatar
Lapo
Site Admin
Posts: 20219
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 06 Mar 2011, 17:05

I think there are some wrong dependencies going on here. I did a quick test locally (I am no Android developer) and it seems to me that you need to use a different logging library. Probably it's our fault, maybe we didn't package the right logging jars. I need to verify

Please drop us an email (Support > Contact us) and we'll guide you in a quick test.
Lapo
--
gotoAndPlay()
...addicted to flash games
krzysztof
Posts: 7
Joined: 08 Mar 2011, 19:10
Location: Poland

Postby krzysztof » 12 Mar 2011, 10:06

I'm wondering if those logging libraries are really needed on Android. Their are quite heavy. I've examined that the simplest application, which does nothing but connecting to the server, already occupies about 1,5 MB of disk space.. :shock:

What are you using those for?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Mar 2011, 10:14

We are using SLF4J - http://www.slf4j.org/

There are various implementations and you can grab the Android one to use that.

But the logging jars here are 29 kb for the api and 8 kb for the implementation jar (simple).

So not sure what you mean by 1.5 mb logging jars?!?!

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
krzysztof
Posts: 7
Joined: 08 Mar 2011, 19:10
Location: Poland

Postby krzysztof » 12 Mar 2011, 11:06

Ok these two are light, but there is log4j-1.2.15 (383kB) included in the API's zip file as well.

I meant that the application which includes all the jars from the zip takes 1.5 MB

Sorry if I'm confusing sth
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Mar 2011, 11:15

I'm pretty sure that you can remove log4j if you are using the simple logging implementation
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
User avatar
Lapo
Site Admin
Posts: 20219
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 14 Mar 2011, 08:46

Yes absolutely!
Lapo

--

gotoAndPlay()

...addicted to flash games
rohana2
Posts: 6
Joined: 03 Feb 2011, 02:15

Postby rohana2 » 05 Apr 2011, 15:47

Anybody able to figure out how to successfully load the config file in an Android app? Any pointers would be appreciated.

thanks,
Andy

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 1 guest