在 Android SignalR 客户端上接收带有自定义对象的消息,数据未反序列化

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

我在 ASP.NET Core 5 Web 服务器上使用 SignalR 进行 Android 设备管理。我可以从设备 (D2C) 发送消息,并接收带有 String 参数的消息 (C2D)。但是我无法接收带有自定义对象参数的消息,处理程序将所有对象成员接收为 null。我开发了一个 WPF 客户端,它很好地接收了这个对象。

我正在关注 ASP.NET Core SignalR Java client documentation. It explains how to use custom objects in Passing Class information in Java 部分。

在 build.gradle 文件中:

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

这是我在 Android 项目中的自定义 class:

package com.mycompany.mayapp.signalr.models;

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

如果这有帮助,这是我在 ASP.NET 核心项目中的自定义 class(如果我使用字段而不是属性,WPF 客户端不起作用,我不知道为什么):

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

这是我的 android 客户 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();
    }    
}

这是我在每个处理程序中得到的输出:

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

你知道我做错了什么吗?

似乎在 java 客户端中,自定义对象字段名称应该是小写的。所以更改字段名称解决了问题。

Android 项目中的自定义 class:

package com.mycompany.mayapp.signalr.models;

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

处理方式:

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