原本以为是BEGIN_OBJECT,但在一行是STRING - GSON。

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

我得到的是以下网址 api:

{"status":true,"ref":"urltogo","url":"http:\/\/myurl\/login\/index.php?testsession=55207"}

这里是我的联系方式 api:

mCompositeCommonDisposable.add(
            login.getResultLogin("YYYY", "YYYY","/")
                !!.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribeWith(object : DisposableObserver<Response<Login?>>() {
                    override fun onNext(loginResponse: Response<Login?>) {
                        if (loginResponse.isSuccessful) {
                            if (loginResponse.body() != null) {
                                loginModelApiResponseResultListener.onSuccess(
                                    loginResponse.body()
                                )
                            } else {
                                loginModelApiResponseResultListener.onFailure(R.string.err_information)
                            }
                        } else {
                            loginModelApiResponseResultListener.onFailure(R.string.err_information)
                        }
                    }

                    override fun onError(e: Throwable) {
                        loginModelApiResponseResultListener.onFailure(R.string.fetching_data_failed)
                    }

                    override fun onComplete() {}
                })

这是我的 interface:

interface PublicApiTS {
    @POST("/theme/fordson2/login")
    fun getResultLogin(
        @Query("username") username: String?,
        @Query("password") password: String?,
        @Query("returnurl") returnurl: String?
    ): Observable<Response<Login?>?>?
}

但它说我。

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $    c

这是我的习俗 object:

@Entity(tableName = "Login")
data class Login (
    @PrimaryKey(autoGenerate = true)
    var id_login: Int = 0,
    @SerializedName("status")
    @Expose
    val status: Boolean? = null,
    @SerializedName("ref")
    @Expose
    val ref: String? = null,
    @SerializedName("url")
    @Expose
    val url: String? = null
)

这是我的改造模块。

@Module
class RemoteAPIDataModuleTS {
    @Provides
    fun provideGson(): Gson {
        return GsonBuilder().setLenient().create()
    }

    @Provides
    fun providePublicApi(retrofit: Retrofit): PublicApiTS {
        return retrofit.create(PublicApiTS::class.java)
    }

    @Provides
    fun provideRetrofitCommon(gson: Gson?): Retrofit {
        val client = OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .build()
        return Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson!!))
            .addConverterFactory(ScalarsConverterFactory.create())
            .baseUrl(API_LOGIN_URL)
            .build()
    }
}
android retrofit2 android-json
1个回答
-1
投票

我编辑了我的 interface 喜欢bellow和良好的工作。

interface PublicApiTS {
    @POST("/theme/fordson2/login")
    @FormUrlEncoded
    fun getResultLogin(
        @Field("username") username: String?,
        @Field("password") password: String?,
        @Field("returnurl") returnurl: String?
    ): Observable<Response<Login?>?>?
}

这里: @Query --> to --> @Field

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