【问题标题】:OkHttp post content is empty in release build after migrate to androidX迁移到 androidX 后,OkHttp 帖子内容在发布版本中为空
【发布时间】:2020-01-12 22:04:17
【问题描述】:

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

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

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

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

[更新]:通过kushancmets的推荐添加接口和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'

【问题讨论】:

  • 贴出声明方法的接口
  • 同时发布 POJO 对象

标签: android android-studio retrofit2 okhttp androidx


【解决方案1】:

以后升级到 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

【讨论】:

  • 是的!我忘了我要做的另一件事是将我的 Android Studio 升级到 3.5,因为很多人都说它很好用!谢谢你的回答!!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2013-05-28
    • 2020-02-03
    相关资源
    最近更新 更多