Signal R Native Android应用程序协商失败

时间:2015-05-27 09:15:28

标签: java c# android asp.net-mvc signalr

我正在尝试创建一个可以连接到我的基本SignalR Hub的Android应用程序。

它只是一个基本的Hub文件,我想测试一下,但到目前为止我还没有运气。有人可以看看我做错了吗?每次我尝试运行它时,我得到以下堆栈:

05-27 09:10:39.785  22247-22247/nl.vict.chatapp I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<microsoft.aspnet.signalr.client.transport.WebsocketTransport$1>
05-27 09:10:39.785  22247-22247/nl.vict.chatapp I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<microsoft.aspnet.signalr.client.transport.WebsocketTransport$1>
05-27 09:10:39.829  22247-22266/nl.vict.chatapp I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<microsoft.aspnet.signalr.client.transport.WebsocketTransport$1>
05-27 09:10:39.830  22247-22247/nl.vict.chatapp W/System.err﹕ java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
05-27 09:10:39.830  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:112)
05-27 09:10:39.830  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:102)
05-27 09:10:39.830  22247-22247/nl.vict.chatapp W/System.err﹕ at nl.vict.chatapp.MainActivity.onCreate(MainActivity.java:48)
05-27 09:10:39.831  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5937)
05-27 09:10:39.831  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-27 09:10:39.832  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-27 09:10:39.832  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-27 09:10:39.832  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-27 09:10:39.833  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-27 09:10:39.833  22247-22247/nl.vict.chatapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
05-27 09:10:39.833  22247-22247/nl.vict.chatapp W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
05-27 09:10:39.833  22247-22247/nl.vict.chatapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
05-27 09:10:39.834  22247-22247/nl.vict.chatapp W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
05-27 09:10:39.834  22247-22247/nl.vict.chatapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ Caused by: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:86)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:82)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ Caused by: java.lang.NoClassDefFoundError: microsoft.aspnet.signalr.client.transport.WebsocketTransport$1
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.transport.WebsocketTransport.start(WebsocketTransport.java:92)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.transport.AutomaticTransport.resolveTransport(AutomaticTransport.java:89)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.transport.AutomaticTransport.start(AutomaticTransport.java:143)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.startTransport(Connection.java:673)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.access$500(Connection.java:30)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.Connection$3.run(Connection.java:373)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.Connection$3.run(Connection.java:350)
05-27 09:10:39.855  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.SignalRFuture.setResult(SignalRFuture.java:73)
05-27 09:10:39.856  22247-22247/nl.vict.chatapp W/System.err﹕ at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:82)
05-27 09:10:39.856  22247-22247/nl.vict.chatapp W/System.err﹕ ... 2 more

这是我的代码:

集线器:

public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.addNewMessageToPage(name, message);
        }
    }

主要活动(Android Studio)

ListView listView;
    HubConnection connection;
    HubProxy proxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView);

        String[] values = new String[]{
                "Testvalue1",
                "Testvalue2",
                "Testvalue3"
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,android.R.id.text1,values);

        listView.setAdapter(adapter);

        connection = new HubConnection("http://www.voffice.nl/chat");
        proxy = connection.createHubProxy("chatHub");
        SignalRFuture<Void> awaitConnection = connection.start();
        try{
            awaitConnection.get();
        }catch (InterruptedException e){
            e.printStackTrace();
        }catch (ExecutionException e){
            e.printStackTrace();
        }



    }

先谢谢,

凯文

-

编辑:

我已检查过服务器并启用了websockets。

1 个答案:

答案 0 :(得分:2)

以下是经过几天的努力后我的工作方式:

  1. 下载https://github.com/TooTallNate/Java-WebSocket并构建它。

  2. 下载https://github.com/SignalR/java-client

  3. 将libs文件夹添加到signalr-client-sdk并放入java_websocket.jar 你之前建立的
  4. 替换signalr-client-sdk / build.gradle

    compile 'org.java-websocket:java-websocket:1.3.1'

    使用:

    compile files('libs/java_websocket.jar')

  5. 转到此处,找到http://到ws:// url https://github.com/SignalR/java-client/issues/63的修复程序,在代码中更改

  6. 构建信号器客户端并将其包含在您的项目中,您还必须包含您在第一步中构建的java_websocket.jar,因此您的gradle文件看起来像:

    compile 'client.signalr.aspnet.microsoft.signalr_client_sdk_android:signalr-client-sdk-android-release:1.0.0@aar' compile files('libs/signalr-client-sdk.jar') compile files('libs/java_websocket.jar')

  7. 不要将任何查询字符串传递给HubConnection,否则它会挂起!!

    如果您需要传递标题(例如授权令牌),则必须修复更多代码,有拉取请求:https://github.com/SignalR/java-client/pull/64