Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用安卓改型与firebase云功能通信?_Android_Kotlin_Google Cloud Functions_Retrofit - Fatal编程技术网

Android 如何使用安卓改型与firebase云功能通信?

Android 如何使用安卓改型与firebase云功能通信?,android,kotlin,google-cloud-functions,retrofit,Android,Kotlin,Google Cloud Functions,Retrofit,我想使用谷歌功能从我的Android应用程序向谷歌Firebase发送一个请求 因此,我在客户端定义了一个API接口,如: interface MyApi { /** * Get the list of the pots from the API */ @GET("/posts") fun getPosts(): Observable<List<Post>> @POST("/addUser") fun addU

我想使用谷歌功能从我的Android应用程序向谷歌Firebase发送一个请求

因此,我在客户端定义了一个API接口,如:

interface MyApi {
    /**
     * Get the list of the pots from the API
     */
    @GET("/posts")
    fun getPosts(): Observable<List<Post>>

    @POST("/addUser")
    fun addUser(@Query("name") name: String, @Query("token") token: String): Observable<User>
}
我观察到的用户对象如下所示:

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.Expose

@Entity
data class User(
        @Expose
        @field:PrimaryKey
        val idUser: Int,
        @Expose
        val token: String,
        @Expose
        val name: String,
        @Expose
        val age: Int
)
exports.addUser = functions.https.onRequest(async (request:any,response:any) => {

  // read parameters from request
  const name = request.query.name
  const token = request.query.token

  const data = {
    name: name,
    age: 30,
    token: token

  }

  // use add if no doc-ID is specified else use set
  const idUser = (await db.collection(collectionUser).add(data)).id

  const newData = {
    idUser: idUser,
  }

  const userRef = db.collection(collectionUser).doc(idUser)
  await userRef.update(newData)
  const newUserRef = await userRef.get()

  response.send(newUserRef.data())
})
我提供改装的网络模块如下所示(仅摘录):

'

@Provides
@Reusable
@JvmStatic
internal fun provideRetrofitInterface(): Retrofit {
    return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()
}
'

@Provides
@Reusable
@JvmStatic
internal fun provideRetrofitInterface(): Retrofit {
    return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()
}
在服务器端,我部署的google云功能如下所示:

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.Expose

@Entity
data class User(
        @Expose
        @field:PrimaryKey
        val idUser: Int,
        @Expose
        val token: String,
        @Expose
        val name: String,
        @Expose
        val age: Int
)
exports.addUser = functions.https.onRequest(async (request:any,response:any) => {

  // read parameters from request
  const name = request.query.name
  const token = request.query.token

  const data = {
    name: name,
    age: 30,
    token: token

  }

  // use add if no doc-ID is specified else use set
  const idUser = (await db.collection(collectionUser).add(data)).id

  const newData = {
    idUser: idUser,
  }

  const userRef = db.collection(collectionUser).doc(idUser)
  await userRef.update(newData)
  const newUserRef = await userRef.get()

  response.send(newUserRef.data())
})
使用postman向云功能发送请求是可行的,但不幸的是,在客户端应用程序中不可行


有什么想法吗?

好的,找到了解决方案。我在fromCallabe部分中添加了方法blockingFirst(),因此它看起来像:

subscription = Observable.fromCallable{ loveDateApi.addUser(name, token).blockingFirst() }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( { result -> println("Result: $result") },
                        {error -> println("Error: $error")})
您知道RxJava专家的一些替代解决方案吗?;)