迁移到 androidX 后,OkHttp post 发布版本中的内容为空

OkHttp post content is empty in release build after migrate to androidX

在迁移到 androidX 之前,发布版本可以很好地处理每个 HTTP 请求。

迁移到androidX后,每个GET请求都正常,但是所有POST请求都不工作。它不能向服务器端发送参数。 POST请求内容全部为空

我所做的唯一更改是将此项目迁移到 androidX。

我不确定在哪里可以找到更多见解...:(

[更新]:根据kushan条评论的推荐添加接口和POJO :)

界面

@POST("/api/preference")
Call<RoasterPreference> updatePreference(@Body RoasterPreference roasterPreference);

POJO 对象

import com.google.gson.annotations.SerializedName;

public class RoasterPreference {

  @SerializedName("type")
  private String type;

  @SerializedName("value")
  private String value;

  public RoasterPreference(String _type,String _value){

    type = _type;
    value = _value;
  }
} 

来自调试构建的 OkHttp 日志:

D/OkHttp: --> POST http://192.168.0.1:9999/api/preference
Content-Type: application/json; charset=UTF-8
Content-Length: 29
{"type":"t_unit","value":"0"}
--> END POST (29-byte body)

来自发布版本的 OkHttp 日志:

D/OkHttp: --> POST http://192.168.0.1:9999/api/preference
Content-Type: application/json; charset=UTF-8
Content-Length: 2
{}
--> END POST (2-byte body)

调试模式下的调试器

发布模式下的调试器

迁移到 androidX 时 build.gradle 中的差异

依赖项

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.exifinterface:exifinterface:1.0.0'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.fragment:fragment:1.1.0'
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
implementation 'androidx.core:core:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.github.HotBitmapGG:RingProgressBar:V1.2.3'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'org.apache.commons:commons-lang3:3.3.2'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.2.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.19.0'

// google services
implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

尝试将 @SerializedName("YOUR_NAME")@Expose 添加到您的 RoasterPreference 中的每个属性。

稍后升级到 Android Studio 3.4 时,R8 是默认打开的。 所以Gson库需要添加一个新的proguard规则。

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

参考:https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg