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:
class InProcessRequestFactory extends AbstractRequestFactory {
//...
public InProcessRequestFactory(Class<? extends RequestFactory> requestFactoryInterface) {
this.requestFactoryInterface = requestFactoryInterface;
deobfuscator =
Deobfuscator.Builder.load(requestFactoryInterface,
Thread.currentThread().getContextClassLoader()).build();
}
//...
}
and
public class Deobfuscator {
//...
public static class Builder {
public static 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<? extends Builder> builderClass = found.asSubclass(Builder.class);
Builder builder = builderClass.newInstance();
return builder;
} catch (ClassNotFoundException e) {
throw new RuntimeException("The RequestFactory ValidationTool must be run for the "
+ clazz.getCanonicalName() + " RequestFactory type");
} catch (InstantiationException e) {
ex = e;
} catch (IllegalAccessException e) {
ex = e;
}
throw new RuntimeException(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:
@Override
public void onPerformSync(final Account account, Bundle extras,
String authority, final ContentProviderClient provider,
final SyncResult syncResult) {
Thread.currentThread().setContextClassLoader(mContext.getClassLoader());
// ...
MyRequestFactory requestFactory = 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)"