Calendar.Events?.get(calendarId,eventId)抛出android.os.NetworkOnMainThreadException错误

问题描述 投票:0回答:1

我有一个问题,如果我使用Google Calendar Java库通过ID搜索事件,它将不返回任何内容并引发错误。我有以下代码:


@Module
class GoogleCalendarsApiServiceModule @Inject constructor(): IGoogleCalendarsApiServiceModule {

    private lateinit var googleCalendarService: Calendar

    private val httpTransport: NetHttpTransport = NetHttpTransport()
    private val subscriptions: Disposable = Disposable.empty()

    init {
        this.subscriptions.apply {
            AndroidContextObservable.context()
                .subscribe { context ->
                    val googleCredential = GoogleCredentialsBuilder.getCredentials(httpTransport, context)
                    googleCalendarService = ServiceBuilder.buildGoogleCalendarService(googleCredential, httpTransport)
                }
        }
    }

    fun finalize() {
        this.subscriptions.dispose()
    }

    override fun getEventById(eventId: String): Observable<Event> {
        return Observable.create { subscriber ->
            val calendarEvents: Calendar.Events? = googleCalendarService.events()

            val event: Event = calendarEvents!!
                .get("primary", eventId)
                .execute()

            subscriber.onNext(event)
            subscriber.onComplete()
        }
    }
}

然后被服务称为上一层映射:

@Module
class GoogleCalendarService @Inject constructor(): IGoogleCalendarService {

    @Inject
    lateinit var googleCalendarsApiServiceModule: GoogleCalendarsApiServiceModule

    init {
        DaggerServiceModuleComponent.create().inject(this)
    }

    override fun getCalendarEventById(eventId: String): Observable<MeetingEvent> {
        return this.googleCalendarsApiServiceModule.getEventById(eventId)
            .map { mapMeetingEvent(it) }
            .onErrorReturn { throw Exception(it) }
    }
}

然后我得到了错误:I/System.out: io.reactivex.rxjava3.exceptions.CompositeException: 2 exceptions occurred.

[当我调试时,问题出在Calendar.Events?.get()没有返回任何结果。我对此感到有些困惑,因为如果我转到https://developers.google.com/calendar/v3/reference/events/get并使用ID为...的交互式API,那么说2ilol72ocluq4i1mvlbqn7egel(此应用程序实际存在并使用的事件ID),以下响应:

{
 "kind": "calendar#event",
 "etag": "\"3168457487134000\"",
 "id": "2ilol72ocluq4i1mvlbqn7egel",
 "status": "confirmed",
 "htmlLink": "https://www.google.com/calendar/event?eid=Mmlsb2w3Mm9jbHVxNGkxbXZsYnFuN2VnZWwgZnJlZW1hbi5tYXJjODgwQG0",
 "created": "2020-03-14T23:32:23.000Z",
 "updated": "2020-03-14T23:32:23.567Z",
 "summary": "TEST123",
 "description": "Test123",
 "location": "IKEA Room",
 "creator": {
  "email": "[email protected]",
  "self": true
 },
 "organizer": {
  "email": "[email protected]",
  "self": true
 },
 "start": {
  "dateTime": "2020-03-14T23:32:00Z"
 },
 "end": {
  "dateTime": "2020-03-15T00:32:00Z"
 },
 "iCalUID": "[email protected]",
 "sequence": 0,
 "reminders": {
  "useDefault": true
 }
}

The same API that is called in the application works when done on the interactive API

有人对导致问题的原因有任何建议吗?

我还尝试过获取列表并根据ID对其进行过滤:

    override fun getCalendarEventsByRoomName(eventId: String): Observable<Event> {
        return Observable.create { subscriber ->
            val now = DateTime(System.currentTimeMillis())
            val events: Calendar.Events? = googleCalendarService.events()

            val event = events!!.list("primary")
                .setQ(eventId) // <-- use the event ID as a query
                .setMaxResults(1)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute()

            subscriber.onNext(event )
            subscriber.onComplete()
        }
    }

但是不会返回任何结果,这与googleCalendarService.events().list()的交互式API中的结果相同>>

编辑:进一步调试显示以下错误:

android.os.NetworkOnMainThreadException

意味着可疑代码行在调用活动之内:

class ViewMeetingEventDataActivity: AppCompatActivity() {

    @Inject lateinit var bundleUtilityModule: BundleUtilityModule
    @Inject lateinit var fragmentUtilityModule: FragmentUtilityModule
    @Inject lateinit var googleCalendarService: GoogleCalendarService

    private val subscriptions: Disposable = Disposable.empty()
    private lateinit var eventId: String
    private lateinit var event: MeetingEvent;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        AndroidContextObservable.setContext(this)
        DaggerServiceModuleComponent.create().inject(this)
        DaggerUtilityModuleComponent.create().inject(this)

        this.eventId = intent.getStringExtra(BundleData.MEETING_EVENT_ID.name)!!
        this.subscriptions.apply {
            googleCalendarService.getCalendarEventById(eventId)
                .subscribe(
                    { meetingEvent: MeetingEvent -> event = meetingEvent; println(meetingEvent.description + "Hello") },
                    {}
                )
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        this.subscriptions.dispose()
    }
}

其中this._subscriptions.apply {}被调用。

我有一个问题,如果我使用Google Calendar Java库通过ID搜索事件,它将不返回任何内容并引发错误。我有以下代码:@Module类...

android kotlin rx-java google-calendar-api
1个回答
0
投票

已通过以下代码设法解决了该问题:

© www.soinside.com 2019 - 2024. All rights reserved.