簡體   English   中英

如何在當前的Eclipse Android項目中導入Gottox / socket.io-java-client api?

[英]How to import the Gottox/socket.io-java-client api in current Eclipse Android Project?

我正在嘗試使用與我的android應用程序(java類)的客戶端進行通信的Socket.io編寫運行在node.js上的JavaScript服務器。 由於我需要結合使用Socket.io(或任何其他有效的websockets)框架的JS服務器和Java客戶端,因此我發現Gottox / socket-io.-java-client實現似乎可以完成這項工作。

問題:我對套接字編程和使用Github項目非常陌生。 我嘗試遵循項目中提到的簡單方法,但是在Eclipse中遇到了構建錯誤 ,特別是與WebSocket.jar和json-org.jar的存檔問題有關。

無法解決此問題,當我遇到一個完全不熟悉的錯誤時,我嘗試將項目導入Android Studio。

我只想確保我一開始就在從事這個項目。 這是我的客戶類的樣子:

package com.example.culami;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONException;
import org.json.JSONObject;

public class AcknowledgeExample implements IOCallback {
    private SocketIO socket;

    /**
     * @param args
     */
    /*public static void main(String[] args) {
        try {
            new AcknowledgeExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public AcknowledgeExample() throws Exception 
    { 
        socket = new SocketIO();
        socket.connect("http://192.168.0.108:3000/", this);

        // Sends a string to the server.
        socket.send("Hello Server");

        // Sends a JSON object to the server.
        socket.send(new JSONObject().put("key", "value").put("key2",
                "another value"));

        // Emits an event to the server.
        socket.emit("event", "argument1", "argument2", 13.37);
    }

    @Override
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(String data, IOAcknowledge ack) {
        System.out.println("Server said: " + data);
    }

    @Override
    public void onError(SocketIOException socketIOException) {
        System.out.println("an Error occured");
        socketIOException.printStackTrace();
    }

    @Override
    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }

    @Override
    public void onConnect() {
        System.out.println("Connection established");
    }

    @Override
    public void on(String event, IOAcknowledge ack, Object... args) {
        System.out.println("Server triggered event '" + event + "'");
    }

}

我導入了socketio.jar甚至WebSocket.jar和json-org.jar,因為似乎也需要這些。 由於我已經花費了無數小時來調試構建問題,因此,對於我做錯了什么或應該如何將此庫納入我的android項目的任何反饋,我們將不勝感激。

注意:我正在使用Android L,API 21和jdk1.7來運行此項目。

相反,您可以添加

socket.io客戶端- 0.1.0.jar

他的http://mvnrepository.com/artifact/com.github.nkzawa/socket.io-client/0.1.0的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM