How to build Android test code for socket.io-client.java

╄→尐↘猪︶ㄣ 提交于 2019-12-23 01:52:29

问题


This question is related to How to build a Maven Android project in eclipse. Using suggestions from @user714965 I was able to build the code in project : https://github.com/nkzawa/socket.io-client.java. Now I am trying to compile an Android test project. I created a new Android project and under Java Build Path specified the socket.io-client project as dependency. But I am getting compilation errors. Eclipse is unable to find Emitter Class. This is the relevant code:

public class SocketTask extends AsyncTask<Void, Void, Void> {

    Socket mSocket = null;
    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            Log.d(TAG, "Connecting to server");
            mSocket = IO.socket("<my tested socket.io server address>");
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {

                Log.d(TAG, "Connected");
            }

        }).on("event", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Log.d(TAG, "Event");
            }

        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Log.d(TAG, "Disconnect");
            }

        });

        mSocket.connect();
        return null;
    }
}

回答1:


It looks like dependent libraries are not resolved. Did you install all of them correctly?

  • engine.io-client 0.3.0
  • Java-WebSocket 1.3.0
  • org.json 20090211 (installed in Android as default)

Emitter is included in engine.io-client.




回答2:


I cannot respond to the comments (new StackOverflow user) but I have gotten the library to work in Android Studio via build.gradle. If you are primarly developing for Android they suggest moving to Android Studio as it will soon be the supported IDE.

EDIT

I can connect to a socketIO server built on nodejs from an android application with this library from nkzawa. Reference to repository.

Add library as gradle dependency:

compile 'com.github.nkzawa:socket.io-client:0.3.0'

I wrote a class to hold my socket:

public class SocketIO{

  Socket sock = null;

  public SocketIO(){
    try{
       sock = IO.socket('<connection string>');

       //Place all events here as per documention
       sock.on(Socket.EVENT_CONNECT, new Emitter.Listener(){
         @Override
         public void call(Object... args){
           System.out.println("connect");
         }
       });

    sock.connect();
    } catch(URISyntaxException e){
      e.printStatckTrace();
    }
  };

  //Helper function for emiting from android
  public void sEmit(String event, JSONObject obj){
    if(socket.connected()){
      sock.emit(event, obj);
    }
  }
}

And use it in my activity:

SocketIO socket = new SocketIO();

I had some initial trouble getting the library to work but it has been flawless since, the "issues" section of the github repository is very helpful.

Hope that helps.



来源:https://stackoverflow.com/questions/26174038/how-to-build-android-test-code-for-socket-io-client-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!