android – 当启用ProGuard时,Retrofit 2不发送数据

android – 当启用ProGuard时,Retrofit 2不发送数据,第1张

概述我尝试使用Retrofit 2登录我的用户(基本上是一个GET到具有基本标题的登录URL)它的效果很好,但是一旦我的ProGuard它,标题授权不再发送了. (见日志输出) 示例代码: 用户型号: public interface UserService { @GET(GET_LOGIN) Observable<User> login(@Header("Authorization 我尝试使用Retrofit 2登录我的用户(基本上是一个GET到具有基本标题的登录URL)它的效果很好,但是一旦我的ProGuard它,标题授权不再发送了. (见日志输出)

示例代码:

用户型号:

public interface UserService {    @GET(GET_LOGIN)    Observable<User> login(@header("Authorization") String basic);}

登录活动:

public voID onClick(VIEw v) {    mRetrofit.create(UserService.class)             .login(Credentials.basic(email,password))             .subscribeOn(Schedulers.io())             .observeOn(AndroIDSchedulers.mainThread())             .subscribe(user -> {                UserHelper.save(LoginActivity.this,user);             },throwable -> Dog.d);}

Proguard文件:

# Retrofit-dontwarn retrofit2.** -dontwarn org.codehaus.mojo.**-keep class retrofit2.** { *; }-keepattributes Signature-keepattributes Exceptions-keepattributes *Annotation*-keepclasseswithmembers class * {    @retrofit.* <methods>;}-keepclasseswithmembers interface * {    @retrofit.* <methods>;}

日志(proguard):

D/Okhttp: --> GET http://passport-supercairos.rhcloud.com/users/login http/1.1D/Okhttp: User-Agent: VirtualPassport-ClIEnt {AndroID-23} {Aquaris_E5}D/Okhttp: Cache-Control: max-stale=10800D/Okhttp: --> END GETD/Okhttp: <-- http/1.1 401 Unauthorized (258ms)D/Okhttp: Date: Fri,19 Feb 2016 12:57:19 GMTD/Okhttp: X-Powered-By: ExpressD/Okhttp: WWW-Authenticate: Basic realm="Users"D/Okhttp: Keep-Alive: timeout=15,max=100D/Okhttp: Connection: Keep-AliveD/Okhttp: transfer-encoding: chunkedD/Okhttp: Content-Type: text/plainD/Okhttp: Okhttp-Sent-Millis: 1455886639681D/Okhttp: Okhttp-Received-Millis: 1455886639787D/Okhttp: UnauthorizedD/Okhttp: <-- END http (12-byte body)

日志(非保固):

D/Okhttp: --> GET http://passport-supercairos.rhcloud.com/users/login http/1.1D/Okhttp: User-Agent: VirtualPassport-ClIEnt {AndroID-23} {Aquaris_E5}D/Okhttp: Cache-Control: max-stale=10800D/Okhttp: Authorization: Basic ZG9yb2ZyanVAZ21haWwuY29tOmN2dnZ2dnY=D/Okhttp: --> END GETD/Okhttp: <-- http/1.1 401 Unauthorized (258ms)D/Okhttp: Date: Fri,max=100D/Okhttp: Connection: Keep-AliveD/Okhttp: transfer-encoding: chunkedD/Okhttp: Content-Type: text/plainD/Okhttp: Okhttp-Sent-Millis: 1455886639681D/Okhttp: Okhttp-Received-Millis: 1455886639787D/Okhttp: UnauthorizedD/Okhttp: <-- END http (12-byte body)

完整的代码可以在这里找到:https://github.com/supercairos/virtual-passport

解决方法 我终于设法使其工作.这是关于Retrofit 2的proguard配置
# Retrofit-dontwarn retrofit2.**-dontwarn org.codehaus.mojo.**-keep class retrofit2.** { *; }-keepattributes Signature-keepattributes Exceptions-keepattributes *Annotation*-keepattributes RuntimeVisibleAnnotations-keepattributes RuntimeInvisibleAnnotations-keepattributes RuntimeVisibleParameterannotations-keepattributes RuntimeInvisibleParameterannotations-keepattributes EnclosingMethod-keepclasseswithmembers class * {    @retrofit2.* <methods>;}-keepclasseswithmembers interface * {    @retrofit2.* <methods>;}

谢谢@ xudshen

UPDATE

主要问题:我用proguard-androID-optimize我应该补充说:

-keepclasseswithmembers class * {    @retrofit2.http.* <methods>;}

我也切换到正方形提供的常规Retrofit 2 proguard配置:

# Platform calls Class.forname on types which do not exist on AndroID to determine platform.-dontnote retrofit2.Platform# Platform used when running on robovm on iOS. Will not be used at runtime.-dontnote retrofit2.Platform$IOS$MainThreadExecutor# Platform used when running on Java 8 VMs. Will not be used at runtime.-dontwarn retrofit2.Platform$Java8# Retain generic type information for use by reflection by converters and adapters.-keepattributes Signature# Retain declared checked exceptions for use by a Proxy instance.-keepattributes Exceptions
总结

以上是内存溢出为你收集整理的android – 当启用ProGuard时,Retrofit 2不发送数据全部内容,希望文章能够帮你解决android – 当启用ProGuard时,Retrofit 2不发送数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131751.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存