简单的SignalR Android示例问题

时间:2016-02-10 16:14:34

标签: android azure websocket signalr signalr-hub

我基本上是尝试从我的android发送消息到我的服务器和服务器发送回我的A​​ndroid应用程序的响应。我跟着THIS tutorial

使用Azure Web API和Android向SignalR介绍自己只是一个简单的练习。

我在C#中的完整服务器代码:

   public class TestHub: Hub {
        public void SendMessage(string name, string message) {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
        public void SendClientMessage(CustomType obj) {
            Clients.All.broadcastMessage("From Server", "Server got the message bro");
        }
        public class CustomType {
            public string Name;
            public int Id;
        }
   }

完整的Android Java代码:

public class MainActivity extends AppCompatActivity {

    Handler handler;
    TextView statustext;

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

        handler = new Handler();
        statustext = (TextView) findViewById(R.id.status);

        Platform.loadPlatformComponent(new AndroidPlatformComponent());
        // Change to the IP address and matching port of your SignalR server.
        String host = "https://My-Service-name.azure-mobile.net/";
        HubConnection connection = new HubConnection(host);
        HubProxy hub = connection.createHubProxy("TestHub");
        SignalRFuture < Void > awaitConnection = connection.start();

        try {
            awaitConnection.get();
        } catch (InterruptedException e) {

        } catch (ExecutionException e) {

        }

        hub.subscribe(this);

        try {
            hub.invoke("SendMessage", "Client", "Hello Server!").get();
            hub.invoke("SendClientMessage",
                new CustomType() {
                    {
                        Name = "Android Homie";
                        Id = 42;
                    }
                }).get();
        } catch (InterruptedException e) {

        } catch (ExecutionException e) {

        }
    }

    //I have no idea what the following method is for. Just followed the tutorial.. (blindly)
    public void UpdateStatus(String status) {
        final String fStatus = status;
        handler.post(new Runnable() {
            @Override
            public void run() {
                statustext.setText(fStatus);
            }
        });
    }

    public class CustomType {
        public String Name;
        public int Id;
    }
}

问题:

1。我得到一个例外:

  

java.util.concurrent.ExecutionException:   microsoft.aspnet.signalr.client.transport.NegotiationException:那里   是与服务器协商的问题

2。我觉得我没有从Java代码中正确地调用服务器。   URL应该是:

  

https://My-Service-name.azure-mobile.net/

  

https://My-Service-name.azure-mobile.net/api/signalr

有人可以澄清这些疑惑并帮我设置吗?

0 个答案:

没有答案