Groovy HTTPBuilder + RxJava返回一个空对象/ observable

时间:2018-04-10 16:01:05

标签: grails groovy rx-java

def login(String email, String password, String apiKey) throws HttpResponseException {

    def postBody = [
            email   : email,
            password: password
    ]

    def http = new HTTPBuilder('https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=' + apiKey)

    return Observable.just({ Observer observer ->

        def thread = new Thread({

            http.post(body: postBody, requestContentType: ContentType.JSON) { resp, json  ->
                println 'status code ' + resp.statusLine.statusCode
                if (resp.statusLine.statusCode == 200 || resp.statusLine.statusCode == 201) {
                    observer.onNext(json)
                } else {
                    observer.onError(new Throwable('broken'))
                }

                observer.onCompleted()

            }

            http.handler.failure = { resp ->
                observer.onError(new Throwable('failure'))
                observer.onCompleted()

            }

            http.handler.'400' = { resp ->
                observer.onError(new Throwable('bad request'))
                observer.onCompleted()

            }

        } as Runnable)

        thread.start()

        return Subscriptions.empty()
    })
}

所以这是我在服务类中编写的方法,用于调用Firebase auth REST API的登录。但现在的事情是,它没有给我任何数据。

我在Intellij中对此进行了调试,我发现当我执行login.subscribe时,我希望它能将json返回给我,但事实并非如此。

那我做错了什么?

1 个答案:

答案 0 :(得分:2)

我不是Groovy开发者,但这条线似乎错了:

return Observable.just({ Observer observer ->

你正在创建一个lambda对象,只是由于内部没有任何内容被执行。你可能正在寻找

return Observable.create({ Emitter emitter -> 

修改

JavaDoc