SignalR的Andr​​oid本地主机的访问主机、SignalR、Andr、oid

2023-09-07 00:23:05 作者:拱手江山只讨你欢

有关某种原因,我不能让让Android应用程序连接到我的服务器(在本地主机)

For some reason I cannot make make android app connect to my server (on localhost)

问题是(可能)不会在code,完全相同的code在一个简单的Java应用程序运行正常。

The problem is (probably) not in the code,the exact same code runs fine in a simple java application

我得到一个InvalidHttpStatus $ C $了CException

I get an InvalidHttpStatusCodeException

错误的请求 - 无效的主机名HTTP错误400请求主机名是  无效的。

Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.

我使用其他模拟器试过,Genymotion(我改变了IP来 http://192.168.56.1 ...)

I tried using another emulator ,Genymotion(I changed the IP to http://192.168.56.1... )

java.util.concurrent.ExecutionException:  microsoft.aspnet.signalr.client.transport.NegotiationException:有  在谈判中与服务器问题

java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server

我在HttpClienTransport.java得到这个例外,76行

I get this Exception in HttpClienTransport.java ,line 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;
}

如果让我从我的浏览器,GET请求我没有得到一个错误

If I make that get request from my browser I don't get an error

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\":false,\"ProtocolVersion\":\"1.3\",\"TransportConnectTimeout\":5.0,\"LongPollDelay\":0.0}

{"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

下面是我的整个code

Here is my entire code

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";
    }
}

对不起,长的帖子,但我想给你所有的细节都

Sorry for the long post but I wanted to give you all the details a have

推荐答案

当我开始我是用这个我的服务器:

When I was starting my server I was using this:

http://localhost:3227

改成了

http://192.168.56.1:3227

和它的工作原理...

And it works...