当我尝试使用谷歌日历 api 时,我在 ICS 上遇到了问题。要获取事件列表,我有一个 AsyncTask 并在 doInBackground() 内部进行服务调用,但仍然收到 NetworkOnMainThreadException 错误。
这是我的代码:
我这样称呼我的 AsyncTask:
new GoogleCalendarEvents().execute(new Object());
我的 AsyncTask 看起来像这样:
private class GoogleCalendarEvents extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... objects) {
OAuthManager manager = OAuthManager.getInstance();
/* login to google api and get the auth token */
manager.doLogin("", true, mActivity, new OAuthManager.AuthHandler() {
@Override
public void handleAuth(Account account, String authToken) {
if (account != null) {
/* create a google Calendar service if the returned account is not null */
com.google.api.services.calendar.Calendar service = CalendarServiceBuilder.build(OAuthManager.getInstance().getAuthToken());
try {
/* get the list of events for the primary calendar */
/* ---------HERE THE APP IS CRUSHING --------- */
mEvents = service.events().list(mCalendarId).execute();
while (true) {
/* get the next page token if any */
String pageToken = mEvents.getNextPageToken();
if (pageToken != null && !pageToken.equals("")) {
/* if there are other pages, get the next page */
Events newPageEvents = service.events().list(mCalendarId)
.setPageToken(pageToken).execute();
for (Event event : newPageEvents.getItems()) {
/* loop through all the events and add them into the mEvents */
mEvents.put(event.getId(), event);
}
} else {
break;
}
}
/* make sure we wont have memory leaks */
mActivity = null;
/* notify the caller that the calendar events are ready */
mEventsReceiverCallback.calendarEventsReceived(true);
mEventsReceiverCallback = null;
} catch (IOException e) {
/* let the user know that the event could not be created */
Utility.showInformationDialog(mActivity, mActivity.getString(R.string.error),
mActivity.getString(R.string.error_message_bad_request_get_events));
/* make sure we wont have memory leaks */
mActivity = null;
/* print the error into the log */
e.printStackTrace();
/* notify the caller that the manager could not get the calendar events */
mEventsReceiverCallback.calendarEventsReceived(false);
mEventsReceiverCallback = null;
}
} else {
/* le the user know that the event could not be created */
Utility.showInformationDialog(mActivity, mActivity.getString(R.string.error),
mActivity.getString(R.string.error_message_connection));
/* make sure we wont have memory leaks */
mActivity = null;
/* notify the caller that the manager could not get the calendar events */
mEventsReceiverCallback.calendarEventsReceived(false);
mEventsReceiverCallback = null;
}
}
});
return null;
}
}
和日志:
02-22 10:32:08.597: ERROR/AndroidRuntime(13924): FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.google.api.client.http.apache.ApacheHttpRequest.execute(ApacheHttpRequest.java:58)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:639)
at com.google.api.client.http.json.JsonHttpClient.execute(JsonHttpClient.java:257)
at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:121)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)
at com.google.api.services.calendar.Calendar$Events$List.execute(Calendar.java:2643)
at /* here is the error, I think */com.google.calendar.manager.business.CalendarEventProvider$GoogleCalendarEvents$1.handleAuth(CalendarEventProvider.java:118)
at com.google.calendar.manager.utils.OAuthManager$1.run(OAuthManager.java:194)
at android.accounts.AccountManager$10.run(AccountManager.java:1264)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
这里说我必须使用 AndroidHttp.newCompatibleTransport() 作为传输类,我确实在我的 CalendarServiceBuilder 中使用它。
什么可能导致此错误以及如何解决该问题?
谢谢!