Android Client doesn't get data but .net clien

2019-08-09 15:34发布

问题:

Apologize for the long question! I am new in SignalR. Please help me to solve my problem. I have SignalR server in web, I am getting data from .net client site but unable to get data from my Android client side. When my debugger arrived to "hub.invoke" is just stop. I have tired a lot with different codes but un-success.

If I unable to do above then I want to do that in my local server at my home. I have two laptops, one is running Win7 another Win8. I want to keep a signalR server in one of them and access from another. How can I do that? what kind of SignalR server API I will write? If you guys please help me step my step?

bellow is my tied code

String android_id = Settings.Secure.getString(arg0.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);

HubConnection con = new   HubConnection(Uri.parse("http://10.198.40.32:8090/login/").toString());
HubProxy hub = con.createHubProxy("NotificationHub");
con.start();                    
try {
    hub.invoke( "userID", "password", android_id).get();
} catch (InterruptedException e) {
    // Handle ...
    System.out.print(e);
} catch (ExecutionException e) {
    // Handle ...
    System.out.print(e);
}

回答1:

You can refer to my working sample code as the following. Then, you can replace Object.class by your own custom class:

public <T> void startSignalR(String transport, String serverUrl, final String userName, final Class<T> tClass) {

    Platform.loadPlatformComponent(new AndroidPlatformComponent());
    Credentials credentials = new Credentials() {
        @Override
        public void prepareRequest(Request request) {
            request.addHeader(HEADER_KEY_USERNAME, userName);
        }
    };

    mConnection = new HubConnection(serverUrl);
    mConnection.setCredentials(credentials);
    mHub = mConnection.createHubProxy(SERVER_HUB_CHAT);

    if (transport.equals("ServerSentEvents")) {
         mTransport = new ServerSentEventsTransport(mConnection.getLogger());
    } else if (transport.equals("LongPolling")) {
         mTransport = new LongPollingTransport(mConnection.getLogger());
    }

    mAwaitConnection = mConnection.start(mTransport);

    try {
        mAwaitConnection.get();
    } catch (InterruptedException e) {      
        e.printStackTrace();      
        return;
    } catch (ExecutionException e) { 
        e.printStackTrace();
        return;
    }

    mHub.on("broadcastMessage",
            new SubscriptionHandler1<Object>() {
                @Override
                public void run(final Object msg) {
                    final String finalMsg;
                    Gson gson = new Gson();
                    Object object = gson.fromJson(msg.toString(), tClass);
                    Field[] fields = object.getClass().getDeclaredFields();
                    for (int i = 0; i < fields.length; i++) {
                        try {
                            System.out.println("Value = " + fields[i].get(object));                                
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }                        
                }
            }
            , Object.class);

    ...
}

A full source code of SignalR for Android/Java, you can find here at GitHub

Hope this helps!