0

I'm accepting a json from the network, in a service.

It notifies an RxBus of the event:

      try {
            String m = msg.getData().getString("message");
            Log.i("handleMessage", m);
            JSONObject message1 = (JSONObject) new JSONTokener(m).nextValue();
            if (_rxBus.hasObservers()) {
                _rxBus.send(new Events.IncomingMsg(message1));
            }

In the subscription side, how do I use that "message1" parameter which is the json i need to manipulate. How do I extract and use the json from the event:

@Override
public void onStart() {
    super.onStart();
    subscriptions = new CompositeSubscription();
    subscriptions//
            .add(bindActivity(this, rxBus.toObserverable())//
                    .subscribe(new Action1<Object>() {
                        @Override
                        public void call(Object event) {
                            if (event instanceof Events.IncomingMsg) {
                                Log.v(TAG,"RXBUS!!!!");
                            }
                        }
                    }));
}
sirvon
  • 2,547
  • 1
  • 31
  • 55
  • the logic seems right. In your check for `instanceof` is it actually logging the message "RXBUS" ? if yes, you're message1 should be available by a simple casting `(Events.IncomingMsg) event` – KG - Jan 18 '15 at 10:27
  • Can create an answer with a simple casting example. – sirvon Jan 19 '15 at 00:01

1 Answers1

3

You can filter it into a stream of JSONObject like so:

(Java 8 lambda style)

rxBus.toObservable()
    .ofType(Events.IncomingMsg.class)
    // I'm making a big assumption that getMessage() is a thing here.
    .map((event) -> event.getMessage())
    .subscribe((message) -> {
        // Do thing with message here!
    });

(Java 7 "classic" style)

rxBus.toObservable()
    .ofType(Events.IncomingMsg.class)
    // I'm making a big assumption that getMessage() is a thing here.
    .map(new Func1<Events.IncomingMsg, JSONObject>() {

        @Override
        public JSONObject call(final Events.IncomingMsg event) {
            return event.getMessage();
        }

    })
    .subscribe(new Action1<JSONObject>() {

        @Override
        public void call(final JSONObject message) {
            // Do something with message here.
        }

    });

(Java 7 "classic" style, filtering "location" string)

rxBus.toObservable()
    .ofType(Events.IncomingMsg.class)
    // I'm making a big assumption that getMessage() is a thing here.
    .map(new Func1<Events.IncomingMsg, String>() {

        @Override
        public String call(final Events.IncomingMsg event) {
            return event.getMessage().getString("location");
        }

    })
    .subscribe(new Action1<String>() {

        @Override
        public void call(final String warehouse) {
            // Do something with warehouse here.
        }

    });
lopar
  • 2,432
  • 22
  • 14
  • Thanks! Few things, rx style is super confusing for as is...don't be brief. Please show all the code that needs to be there. Java 7 translation is needed. – sirvon Jan 19 '15 at 23:59
  • Sure thing, will edit right now. :) I've actually found j8 lambda-style easier to read (definitely easier to write!) for the most part, but I've also had time to get used to the Func# and Action# methods, which I know can be daunting at first. – lopar Jan 20 '15 at 00:01
  • in the broadcast receiver way here's how i would get the json in onReceive....message = (JSONObject) new JSONTokener(intent.getStringExtra("message")).nextValue(); warehouse = message.getString("location"); can you mimic that – sirvon Jan 20 '15 at 00:08
  • I made an attempt. Is that what you're looking for? – lopar Jan 20 '15 at 00:12
  • Is event in the map call the original jsonobject "message1" that was sent on the bus? – sirvon Jan 20 '15 at 00:15
  • Yes, although now both examples emit the string fetched out of "location" in the message to subscribe, not the message itself. – lopar Jan 20 '15 at 00:17
  • ahhh i want the message streamed out. Now this is enough for me to proceed w. Ty. Can you keep both examples...fecthing the whole message and only one variable. Ty very much!! – sirvon Jan 20 '15 at 00:19