我在Android的EndpointAsyncTask
中使用了这种方法:
class EndpointsAsyncTask extends AsyncTask<Void, Void, List<User>> {
private static UserEndpoint myApiService = null;
private Context context;
EndpointsAsyncTask(Context context) {
this.context = context;
}
继续在这里:
@Override
protected List<User> doInBackground(Void... params) {
if(myApiService == null) { // Only do this once
UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://localhost:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver
myApiService = builder.build();
}
但是这只是在Android Studio 1.2.2
只在运行时btw:
9770-9892/com.kkoci.shairlook E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NoClassDefFoundError: com.kkoci.shairlook.EndpointsAsyncTask$1
at com.kkoci.shairlook.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:33)
at com.kkoci.shairlook.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:22)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
我已经在doInBackground
无法使用任何UI
方法的地方读过,可能是吗?
但实际上我无法在这里弄明白,我从一个活动中调用这个AsyncTask类,这样:
public void getUSer(View v) {
new EndpointsAsyncTask(this).execute();
}
这是从按钮调用的,如下所示:
<Button
android:id="@+id/twitter"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Register with Twitter"
android:textColor="#FFF"
android:layout_marginBottom="10dp"
android:textSize="20dp"
android:layout_marginTop="11dp"
android:layout_marginLeft="30dp"
android:background="@drawable/twitter_button"
android:onClick="getUser"/>
有什么想法吗?
提前感谢!