【问题标题】:SignalR java client hubConnection.on() method not trigger any method after receiving dataSignalR java客户端hubConnection.on()方法接收数据后不触发任何方法
【发布时间】:2021-02-09 04:13:23
【问题描述】:

我在我的 Android 应用程序中使用 SignalR 来接收实时数据。当我的“hubConnection.on()”方法启动时,它成功接收数据,但在此方法中它不允许我使用该数据。就像我写这样的代码:

   hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

    hubConnection.start();

    hubConnection.on("ReceiveLocation", (lat, lng) -> {

        Log.i("dataCheck", "-> "+Double.parseDouble(lat));
        Log.i("dataCheck", "-> "+Double.parseDouble(lng));
        if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
            hubConnection.start();

    }, String.class, String.class);

以上案例数据接收成功,也打印在Log中。

但是当我尝试使用这个“lat”、“lng”变量时,就像我尝试用“lat”值显示一个 toast 时,数据没有接收到这个方法并且 toast 也没有显示在屏幕上。就像我使用这样的代码:

        hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

    hubConnection.start();

    hubConnection.on("ReceiveLocation", (lat, lng) -> {


        Toast.makeText(OrderTrackingActivity.this, "->>>"+Double.parseDouble(lat), Toast.LENGTH_SHORT).show();


        Log.i("dataCheck", "-> "+Double.parseDouble(lat));
        Log.i("dataCheck", "-> "+Double.parseDouble(lng));
        if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
            hubConnection.start();

    }, String.class, String.class);

在此第二个代码中,数据未接收且 toast 未显示。请向我建议解决此问题的可能解决方案。我需要使用这些即将到来的数据。

按照建议,我尝试将“lat”、“lng”(.on() 函数的变量)的值存储到我自己的定义变量中,然后将这些变量传递给我的函数名称“myFuntion”,如下所示:

 hubConnection.on("ReceiveLocation", (lat, lng) -> {

        varbl1 = lat;
        varbl2 = lng;
    Log.i("dataCheck", "-> "+Double.parseDouble(lat));
    Log.i("dataCheck", "-> "+Double.parseDouble(lng));

    myFuntion(varbl1 , varbl2);

    if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();

}, String.class, String.class);

然后在“myFuntion”中,我记录值并成功打印值,但当我使用该值时,要么显示在 Toast 中,要么执行其他操作,signalR“hubConnection.on()”功能停止工作。这是我的“myFuntion”方法代码:

 public void myFuntion(String value, String value2){
   
    Toast.makeText(this, ""+value, Toast.LENGTH_SHORT).show();

}

【问题讨论】:

  • 您是否尝试过在使用之前将接收到的变量分配给您的变量?喜欢myLat = lat
  • @Kiril1512 我尝试将此变量提供给我的函数,但它不起作用。让我通过将这些变量值赋予我的定义变量来检查。
  • @Kiril1512 请检查我上次更新的问题,我使用了你的建议,但我仍然面临问题

标签: java android signalr signalr.client


【解决方案1】:

这个问题是因为UI Thread不是Signalr,你可以试试这个代码

            hubConnection.on("ReceiveTxt",(txt)-> {
            Activity activity = (Activity) context;
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(context, "->>> :" + txt, Toast.LENGTH_SHORT).show();
                }
            });
        }, String.class);

如果您在 Activity 中,请使用此

hubConnection.on("ReceiveTxt",(txt)-> {
        this.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(this, "->>> :" + txt, Toast.LENGTH_SHORT).show();
            }
        });
    }, String.class);

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多