简体   繁体   English

在 Android SignalR 客户端上使用自定义 object 接收消息,数据未反序列化

[英]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.

相关问题 接收清单 <Object> 在android studio和SignalR中 - Receive List<Object> in android studio and SignalR 如何使用Microsoft Java-Client接收SignalR广播消息? - How to receive SignalR broadcast message using Microsoft Java-Client? Android客户端不获取数据,但.net客户端从SignalR服务器获取数据 - Android Client doesn't get data but .net client getting data from SignalR server 反序列化并非始终属于同一类型的 json object - Deserializing a json object which isn't always of the same type 使用Websocket Android客户端异步接收文本消息[2] - Receive text message asynchronously with Websocket Android Client [2] 服务器未将消息转发给客户端 - Server isn't forwarding the message to the client Android应用没有从SignalR中心接收数据 - Android app did not receive data from SignalR hub 房间 (android) 中的数据访问 Object Class 无法正常工作 - Data Access Object Class in Room (android) isn't functioning 如何在 twilio android 客户端上接收传入消息? 有什么办法可以在安卓客户端上接收短信/彩信 - How to receive incoming message on the twilio android client? Is there any way to receive sms/MMS on android client 为什么receive()方法不从客户端接收数据? - Why receive() method doesn't receive data from client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM