简体   繁体   中英

SignalR Android access localhost

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

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

I get an InvalidHttpStatusCodeException

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

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: There was a problem in the negotiation with the server

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

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

The response is

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

In both cases I can access other services on the server so the problem is related to signalR

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

Changed it to

http://192.168.56.1:3227

And it works...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM