Skip to content
Advertisement

Receive message with a custom object on Android SignalR client, data isn’t deserializing

I’m using SignalR on ASP.NET Core 5 web server for Android device management. I can send messages from device (D2C), and receive messages with String parameters (C2D). But I can’t receive messages with custom object parameters, the handler receives all object members as null. I develop an WPF client and it receives this object well.

I’m following ASP.NET Core SignalR Java client documentation. It explains how to use custom objects in Passing Class information in Java section.

In build.gradle file:

dependencies {
    ...
    implementation 'com.microsoft.signalr:signalr:5.0.5'
    implementation 'org.slf4j:slf4j-jdk14:1.7.25'
}

This is my custom class in Android project:

package com.mycompany.mayapp.signalr.models;

public class CustomClass
{
    public String Param1;
    public String Param2;
}

If this can help, this is my custom class in ASP.NET Core project (if I use fields instead of properties WPF client doesn’t work, I don’t know why):

namespace MyWebWithSignalRCore.SignalR.Models
{
    public class CustomClass
    {
        public string Param1 { get; set; }
        public string Param2 { get; set; }
    }
}

And this is my android client class:

package com.mycompany.mayapp.signalr;

import android.util.Log;
import com.fagorelectronica.fagorconnectservice.signalr.models.UpdateAppParams;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.OnClosedCallback;
import com.microsoft.signalr.TypeReference;    
import java.lang.reflect.Type;

public class SignalRClient
{
    private static final String TAG = SignalRClient.class.getSimpleName();
    HubConnection hubConnection;

    public SignalRClient(String url)
    {
        this.hubConnection = HubConnectionBuilder.create(url).build();
        this.handleIncomingMethods();
    }
    private void handleIncomingMethods()
    {
        this.hubConnection.on("ReceiveMessage", (user, message) -> { // OK
            Log.d(TAG, user + ": " + message);
        }, String.class, String.class);

        this.hubConnection.on("Reset", () -> { // OK
            Log.d(TAG, "Reset device");
        });

        Type customClassType = new TypeReference<CustomClass>() { }.getType();
        this.hubConnection.<CustomClass>on("Custom", (params) -> { // NOK!!
            Log.d(TAG, params.Param1 + ": " + params.Param2);
        }, customClassType);
    }

    public void start()
    {
        this.hubConnection.start().blockingAwait();
        this.hubConnection.send("Hello", "My device ID"); // OK
    }
    public void stop()
    {
        this.hubConnection.stop().blockingAwait();
    }    
}

This is the output I get in each handler:

D/SignalRClient: User: message
D/SignalRClient: Reset device
D/SignalRClient: null: null

Do you know what I’m doing wrong?

Advertisement

Answer

It seems that in the java client the custom object field names should be in lowercase. So changing the field names solves the problem.

Custom class in Android project:

package com.mycompany.mayapp.signalr.models;

public class CustomClass
{
    public String param1;
    public String param2;
}

Handling method:

private void handleIncomingMethods()
{
    // ... other methods ...

    Type customClassType = new TypeReference<CustomClass>() { }.getType();
    this.hubConnection.<CustomClass>on("Custom", (params) -> { // OK!!
        Log.d(TAG, params.param1 + ": " + params.param2);
    }, customClassType);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement