androidretrofit2android-json

Expected BEGIN_OBJECT but was STRING at line - GSON


I am getting bellow url from api:

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

And here my connection 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() {}
                })

And here is my interface:

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

But it say me:

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

And here is my custom 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
)

And here is my retrofit module:

@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()
    }
}

Solution

  • I edit my interface to like bellow and good work:

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

    Here: @Query ---> to ---> @Field