question

plin avatar image
plin asked sunxishan edited

How to call getOrder in onOrderUpdated callback?

Hi,

I created a service that will listen to the onOrderUpdated, which is working and the callback will be triggered every time when order get touched. I am able to get the orderId but when I try to call orderConnector.getOrder(String orderId) in the callback. It will show the RemoteException.

I was wondering how to get the order detail every time when the order updated?

public class OrderConnectorService extends Service {
    private Account mAccount;
    private OrderConnector orderConnector;
    private OrderChangeListener orderChangeListener;

    private class OrderChangeListener implements OrderV31Connector.OnOrderUpdateListener {
        @Override public
        void onOrderUpdated(String orderId, boolean selfChange) {
            Log.d("OrderChangeListener", orderId);
            Log.d("OrderChangeListener", String.valueOf(selfChange));<br>            // I am able to get orderId and selfChange boolean flag<br>            // Trying to call orderConnector.getOrder here but failed
        }
    }

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        mAccount = CloverAccount.getAccount(this);

        if(mAccount != null && orderConnector == null) {
            orderConnector = new OrderConnector(this, mAccount, null);
            orderChangeListener = new OrderChangeListener();
            orderConnector.addOnOrderChangedListener(orderChangeListener);
            orderConnector.connect();
        }

        return START_NOT_STICKY;
    }


    @Override
    public void onDestroy () {
        orderConnector.disconnect();
        if(orderChangeListener != null) {
            orderConnector.removeOnOrderChangedListener(orderChangeListener);
        }
        orderConnector = null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

There is another BroadcastReceiver will captured "OrderCreated" intent and call startService of above service.

Orders
1 comment
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Marginian avatar image David Marginian ♦♦ commented ·
The details of the remote exception would be helpful.
1 Like 1 ·

1 Answer

·
sunxishan avatar image
sunxishan answered sunxishan edited

I got similar error as OP. I traced down all the way get the exception:

 java.lang.IllegalThreadStateException: service invoked on main thread
     at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:257)

the source code:

protected <T> T execute(ServiceCallable<S, T> callable) throws RemoteException, ClientException, ServiceException, BindingException {
  if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
    throw new IllegalThreadStateException("service invoked on main thread");
  }
...

It seems that we cannot use OrderConnector twice in the same service thread?


Any suggestion? thanks


Update: problem solved. You need another thread to run the order information pulling.

Each thread can only have one orderConnector.

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community