SignalR Android 访问本地主机
SignalR Android access localhost
出于某种原因,我无法使 android 应用程序连接到我的服务器(在本地主机上)
问题(可能)不在代码中,完全相同的代码在简单的 java 应用程序中运行良好
我得到一个 InvalidHttpStatusCodeException
Bad Request - Invalid Hostname HTTP Error 400. The request hostname is
invalid.
我尝试使用另一个模拟器 Genymotion(我将 IP 更改为 http://192.168.56.1...)
java.util.concurrent.ExecutionException:
microsoft.aspnet.signalr.client.transport.NegotiationException: There
was a problem in the negotiation with the server
我在 HttpClienTransport.java 的第 76 行
中得到了这个异常
@Override
public SignalRFuture<NegotiationResponse> 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<NegotiationResponse> negotiationFuture = new SignalRFuture<NegotiationResponse>();
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":"/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":false,"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<String, String, String>
{
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<Void> 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
而且有效...
出于某种原因,我无法使 android 应用程序连接到我的服务器(在本地主机上)
问题(可能)不在代码中,完全相同的代码在简单的 java 应用程序中运行良好
我得到一个 InvalidHttpStatusCodeException
Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.
我尝试使用另一个模拟器 Genymotion(我将 IP 更改为 http://192.168.56.1...)
java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
我在 HttpClienTransport.java 的第 76 行
中得到了这个异常@Override
public SignalRFuture<NegotiationResponse> 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<NegotiationResponse> negotiationFuture = new SignalRFuture<NegotiationResponse>();
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":"/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":false,"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<String, String, String>
{
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<Void> 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
而且有效...