[英]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.我在 ASP.NET 核心 5 web 服务器上使用 SignalR 来管理 ZE84E30B9390CDB64DB6ZDB2C9。 I can send messages from device (D2C), and receive messages with String
parameters (C2D).我可以从设备(D2C)发送消息,并接收带有String
参数的消息(C2D)。 But I can't receive messages with custom object parameters, the handler receives all object members as null.但是我无法接收带有自定义 object 参数的消息,处理程序接收所有 object 成员作为 null。 I develop an WPF client and it receives this object well.我开发了一个 WPF 客户端,它很好地接收了这个 object。
I'm following ASP.NET Core SignalR Java client documentation.我正在关注ASP.NET 核心 SignalR Java 客户端文档。 It explains how to use custom objects in Passing Class information in Java section.它在 Java 部分的传递 Class 信息中解释了如何使用自定义对象。
In build.gradle file:在 build.gradle 文件中:
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:这是我在 Android 项目中的自定义 class :
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):如果这有帮助,这是我在 ASP.NET 核心项目中的自定义 class (如果我使用字段而不是属性 WPF 客户端不起作用,我不知道为什么):
namespace MyWebWithSignalRCore.SignalR.Models
{
public class CustomClass
{
public string Param1 { get; set; }
public string Param2 { get; set; }
}
}
And this is my android client class:这是我的 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();
}
}
This is the output I get in each handler:这是我在每个处理程序中得到的 output:
D/SignalRClient: User: message
D/SignalRClient: Reset device
D/SignalRClient: null: null
Do you know what I'm doing wrong?你知道我做错了什么吗?
It seems that in the java client the custom object field names should be in lowercase.似乎在 java 客户端中,自定义 object 字段名称应为小写。 So changing the field names solves the problem.因此更改字段名称可以解决问题。
Custom class in Android project: Android项目中的自定义class:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.