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

时间:2019-09-11 05:43:13

标签: android android-studio retrofit2 okhttp 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)

处于调试模式的调试器

enter image description here

处于发布模式的调试器

enter image description here

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

enter image description here

依赖性

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'

2 个答案:

答案 0 :(得分:3)

稍后再升级到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>;
}

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

答案 1 :(得分:0)

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