Skip to content Skip to sidebar Skip to footer

How To Use The Gwt-requestfactory In Android Syncadapter (always Getting Validationtool-error)

I got a question about the usage of GWT-RequestFactory in Android. As a starting point I used the code from the “Create a AppEngine connected Android-Project”-Wizard (infos: ht

Solution 1:

You are right Mark, this is an class-loader issue.

It happens in the requestfactory-client.jar, here the relevant source:

classInProcessRequestFactoryextendsAbstractRequestFactory {

    //...publicInProcessRequestFactory(Class<? extends RequestFactory> requestFactoryInterface) {
        this.requestFactoryInterface = requestFactoryInterface;
        deobfuscator =
            Deobfuscator.Builder.load(requestFactoryInterface,
                Thread.currentThread().getContextClassLoader()).build();
    }

    //...

}

and

publicclassDeobfuscator {

    //...publicstaticclassBuilder {
        publicstatic Builder load(Class<?> clazz, ClassLoader resolveClassesWith) {
            Throwable ex;
            try {
                Class<?> found;
                try {
                    // Used by the server
                    found = Class.forName(clazz.getName() + GENERATED_SUFFIX, false, resolveClassesWith);
                } catch (ClassNotFoundException ignored) {
                    // Used by JRE-only clients
                    found = Class.forName(clazz.getName() + GENERATED_SUFFIX_LITE, false, resolveClassesWith);
                }
                Class<? extendsBuilder> builderClass = found.asSubclass(Builder.class);
                Builderbuilder= builderClass.newInstance();
                return builder;
            } catch (ClassNotFoundException e) {
                thrownewRuntimeException("The RequestFactory ValidationTool must be run for the "
                    + clazz.getCanonicalName() + " RequestFactory type");
            } catch (InstantiationException e) {
            ex = e;
        } catch (IllegalAccessException e) {
            ex = e;
        }
        thrownewRuntimeException(ex);
    }

    //...

}

The problem is that Thread.currentThread().getContextClassLoader() seems to return a null value when called from the sync adapter on Android, because the Sync Adapter thread is created by the System, not by your application.

I solved this by calling manually calling setContextClassLoader in the onPerformSync method before creating a requestfactory instance:

@OverridepublicvoidonPerformSync(final Account account, Bundle extras,
        String authority, final ContentProviderClient provider,
        final SyncResult syncResult) {

    Thread.currentThread().setContextClassLoader(mContext.getClassLoader());

    // ...MyRequestFactoryrequestFactory= Util.getRequestFactory(mContext,
            MyRequestFactory.class, acsidToken);

    // ...
}

I hope this makes sense. Andreas

Post a Comment for "How To Use The Gwt-requestfactory In Android Syncadapter (always Getting Validationtool-error)"