Question

I'm trying to attach a SpiceManager to service, but I'm getting:

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.octo.android.robospice.SpiceService$SpiceServiceBinder
        at com.octo.android.robospice.SpiceManager$SpiceServiceConnection.onServiceConnected(SpiceManager.java:1072)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java)
        at android.os.Handler.handleCallback(Handler.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
        at dalvik.system.NativeStart.main(Native Method)

the service flow looks somewhat like this:

class MyService extends Service {
    // ...
    SpiceManager mSpiceManager = new SpiceManager(UncachedSpiceService.class);
    // ...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mSpiceManager.start(this);
        mSpiceManager.execute(new MyRequest(), new RequestListener<String>() {
            // Handle of request
        })
        // ...
        return START_STICKY;
    }

    // + additional calls to mSpiceManager.execute() in other methods
}

This gives me that class cast exception as soon as service starts?

Était-ce utile?

La solution

To use RoboSpice, the entity (context) using a SpiceManager and the SpiceService must live inside the same process.

RoboSpice acts like a bridge between an Android Service and any other context. And this bridge is Object Oriented, allowing an object based bi-directional communication channel between the context and the service.

That's possible on Android at one single condition : the SpiceService and the entity using the SpiceManager must live in the process. That very special "trick" is a hard coded behavior in Android, called "Local Service Bindbing".

If this condition doesn't hold, it's not possible to bind to a RS service in the correct way. When the SpiceManager will bind to the SpiceService it will then receive a BinderProxy with which it is not possible to share references : all communications will have to be based on Bundles, serializable or parcelable objects. In such condition, RS won't work at all.

I have looked at your project on https://github.com/rovo89/XposedBridge/blob/master/src/de/robv/android/xposed/XposedBridge.java and it looks like you are playing with quite low level things in it, and launching new Zygote processes from there. I assume the processes that you create by forking zygote won't be inside the same process as the SpiceService itself, that's the reason why you get this exception.

I am deeply sorry to say so, but you can't use RoboSpice in this project. If you ever find a workaround, I would be quite interested to hear from you, but I don't think it's gonna be possible, by design of Android itself.

Autres conseils

You cannot call a background service/thread from a background thread.

You can achieve it like this.

public void callService() {
        try {
            org.springframework.web.client.RestTemplate rt = new org.springframework.web.client.RestTemplate();
            rt.getMessageConverters().add(
                    new MappingJacksonHttpMessageConverter());
            String url = "xyz";

            Contacts contacts = rt.postForObject(url, body,
                    Contacts.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "Called-" + e.toString());
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top