Extension Error - Only the original thread that created a view hierarchy can touch it views

I’m building a extension for Binance websocket. I am getting the following error even though I started it with Thread. What should I do?

Screenshot_1

  @SimpleFunction
  public void GetPrice(String symbol){

      this.activity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
              client.onAggTradeEvent(symbol.toLowerCase(), new BinanceApiCallback<AggTradeEvent>() {
                  @Override
                  public void onResponse(final AggTradeEvent response) {
                      Price(response.toString());
                  }

                  @Override
                  public void onFailure(final Throwable cause) {
                      Fail("Web Socket Failed");
                      cause.printStackTrace(System.err);
                  }
              });
          }
      });
  }

Its because, onResponse & onFailure method is still new threads / background threads. As, to call an event, you should pass value from UI thread. So, I called events from UI thread using runOnUiThread method of Activity class.

 client.onAggTradeEvent(symbol.toLowerCase(), new BinanceApiCallback<AggTradeEvent>() {
                  @Override
                  public void onResponse(final AggTradeEvent response) {
                      activity.runOnUiThread(new Runnable(){
                          Price(response.toString());
                      });
                     
                  }

                  @Override
                  public void onFailure(final Throwable cause) {
                     activity.runOnUiThread(new Runnable(){
                         Fail("Web Socket Failed"); // or do it like , Fail(cause.getErrorMessage()); That will help user to know error more specefic
                         //cause.printStackTrace(System.err);
                     });
                  }
              });
3 Likes

it’s worked! thank you :blush::blush::blush:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.