Unable to parse by gson with signed APK

Asked 2 years ago, Updated 2 years ago, 112 views

environment:kotlin 1.2.51/gson 2.4/okhttp 3.10 retrofit 2.4

Retrofit and okhttp retrieve json data from WebAPI and store it in data class in converter.
In debug/release mode, it is successfully stored on a member of the data class, but it is not successfully stored on an APK signed by the Generate Signed APK in Android Studio.

Here's the code:

class ServiceGenerator{
US>companion object {
    create (auth:Boolean=false):Router {
        valclient=httpClientBuilder(auth)
        valgson=GsonBuilder()
                        .setFieldNamingPolicy (FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                        .create()
        valueAdapter=RxJava2CallAdapterFactory.createAsync()

        valbaseUrl = LocalConfigManager.getProperty (ConfigConstants.KEY_BASE_URL)
        return Retrofit.Builder()
                .baseUrl (baseUrl!!)
                .addCallAdapterFactory(adapter)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build()
                .create (Router::class.java)
    }

    private fun httpClientBuilder (auth:Boolean):OkHttpClient{
        valclient=OkHttpClient.Builder()
        client.addNetworkInterceptor(HeaderInterceptor())
        if(auth)
            client.addNetworkInterceptor (AuthorizationHeaderInterceptor())
        client.protocols(Arrays.asList(Protocol.HTTP_1_1))

        if(BuildConfig.DEBUG) {
            val logger = HttpLoggingInterceptor()
            // logger.level = HttpLoggingInterceptor.Level.HEADERS
            logger.level = HttpLoggingInterceptor.Level.BODY
            client.addNetworkInterceptor (logger)
        }
        return client.build()
    }
}

interface Router {
    @ GET("/api/v2/settings")
    fun settings():Observable<Response<Foo>>
}

data class Settings (value version: Int, @SerializedName("settings") val details: Setting)
US>data class Setting(
    values —List<Category>=listOf(),
    val personalEntries —List<PersonalEntry>)

From

ServiceGenerator
            .create()
            .beaconContents()
            .compose(bindToLifecycle())
            .observeOn (AndroidScheduler.mainThread())
            .subscribe(
                    {
                        if(it.isSuccessful){
                            it.body()?.let {data->
                                Log.d(TAG, Integer.toString(data.version))<=0 for signed APK.2 in debug/release mode.
                                if(data.beaconContents==null)<=Null on signed APK.A signature APK stores data in an array.
                                    Log.e(TAG, "beacon contents is null.")

                                contentModel?.save(data.beaconContents)
                            }
                        } else {
                            When(it.code()){
                                404 - > Log.e (TAG, "not found.")
                            }
                        }
                    },
                    {
                        Log.e(TAG, it.localizedMessage)
                    })

API Retrieved json Data

{"version":2, "settings":{"categories":[{"id":1, "disp_no":1, "...}

Among the above codes, if you log in the caller subscribe, the signature APK will have version==0 and categories==null.

Does anyone have a similar experience and know how to solve it?

android kotlin retrofit

2022-09-29 22:37

1 Answers

It's solved.
The reason was that code compression was enabled.I was able to parse successfully by specifying @SerializedName


2022-09-29 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.