SignalR Android访问localhost

出于某种原因,我不能让make android app连接到我的服务器(在localhost上)

问题是(可能)不在代码中,完全相同的代码在简单的Java应用程序中运行良好

我得到一个InvalidHttpStatusCodeException

错误请求 – 无效主机名HTTP错误400.请求主机名无效。

我尝试使用另一个模拟器,Genymotion(我将IP更改为http://192.168.56.1 ……)

java.util.concurrent.ExecutionException:microsoft.aspnet.signalr.client.transport.NegotiationException:与服务器协商时出现问题

我在第76行的HttpClienTransport.java中得到了这个例外

@Override public SignalRFuture negotiate(final ConnectionBase connection) { log("Start the negotiation with the server", LogLevel.Information); String url = connection.getUrl() + "negotiate" + TransportHelper.getNegotiateQueryString(connection); Request get = new Request(Constants.HTTP_GET); get.setUrl(url); get.setVerb(Constants.HTTP_GET); connection.prepareRequest(get); final SignalRFuture negotiationFuture = new SignalRFuture(); log("Execute the request", LogLevel.Verbose); HttpConnectionFuture connectionFuture = mHttpConnection.execute(get, new ResponseCallback() { public void onResponse(Response response) { try { log("Response received", LogLevel.Verbose); throwOnInvalidStatusCode(response); log("Read response data to the end", LogLevel.Verbose); String negotiationContent = response.readToEnd(); log("Trigger onSuccess with negotiation data: " + negotiationContent, LogLevel.Verbose); negotiationFuture.setResult(new NegotiationResponse(negotiationContent, connection.getJsonParser())); } catch (Throwable e) { log(e); negotiationFuture.triggerError(new NegotiationException("There was a problem in the negotiation with the server", e)); } } }); FutureHelper.copyHandlers(connectionFuture, negotiationFuture); return negotiationFuture; } 

如果我从浏览器中获取请求,则不会出现错误

url = http://192.168.56.1:3227/signalr/signalr/negotiate?clientProtocol=1.3&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D

回应是

{ “URL”: “/ signalr / signalr”, “ConnectionToken”: “AQAAANCMnd8BFdERjHoAwE / CL + sBAAAAIAYX1zrB / E6bvU / + 2LTe0AAAAAACAAAAAAADZgAAwAAAABAAAAA29atQ2Tga6k2bR6Mj5efoAAAAAASAAACgAAAAEAAAAKhFvUYNaIgEJ / HEE3XK4FAoAAAACmVl4WixdW9X7qc0dfXAtded / ggdT9WGET + 35bQhvjfvdA6jgagBEhQAAADaDg / ev9SXE + bWYFGk10CP + OmCYQ ==”, “的ConnectionId”:“982a12db- e4c8-4234-894a-3d1648e662d5″ , “的KeepAliveTimeout”:20.0, “DisconnectTimeout”:30.0, “TryWebSockets”:假 “ProtocolVersion”: “1.3”, “TransportConnectTimeout”:5.0 “LongPollDelay”:0.0}

在这两种情况下,我都可以访问服务器上的其他服务,因此问题与signalR有关

这是我的整个代码

 import java.util.concurrent.ExecutionException; import microsoft.aspnet.signalr.client.Platform; import microsoft.aspnet.signalr.client.SignalRFuture; import microsoft.aspnet.signalr.client.http.android.AndroidPlatformComponent; import microsoft.aspnet.signalr.client.hubs.HubConnection; import microsoft.aspnet.signalr.client.transport.ServerSentEventsTransport; import android.os.AsyncTask; public class MyTask extends AsyncTask { private String UserName ="AndroidUser"; private microsoft.aspnet.signalr.client.hubs.HubProxy HubProxy; //String ServerURI = "http://192.168.56.1:3227/signalr"; String ServerURI = "http://10.0.2.2:3227/signalr"; private HubConnection Connection ; SignalRFuture con; @Override protected String doInBackground(String... params) { AndroidPlatformComponent com=new AndroidPlatformComponent(); Platform.loadPlatformComponent(com); Connection = new HubConnection(ServerURI); HubProxy = Connection.createHubProxy("MyHub"); try { con =Connection.start(new ServerSentEventsTransport(Connection.getLogger())); //Or LongPollingTransport con.get(); } catch (ExecutionException e) { e.printStackTrace(); } return "blabla"; } } 

对不起,很长的post,但我想给你所有的细节

当我启动我的服务器时,我使用的是:

 http://localhost:3227 

改为

 http://192.168.56.1:3227 

它有效……