android – 复古适合的反应与职业后卫崩溃

我有一个应用程序,使用apit调用改造.我正在尝试添加专业防守,但它仍然失败了其中一个响应.

FATAL EXCEPTION: main Process: com.karriapps.smartsiddurlite, PID: 13387
  java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
       at com.karriapps.smartsiddur.util.b$3.a(SourceFile:255)
       at com.karriapps.smartsiddur.util.b$3.success(SourceFile:252)
       at retrofit.CallbackRunnable$1.run(SourceFile:45)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5431)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

我知道响应包含数据的事实,它似乎无法反序列化它

这是失败的代码

mElavationService.getElevation(new ElavationService.LatLng(mLocation.getLatitude(), mLocation.getLongitude()),
            BING_KEY,
            new Callback<BingElevationResponse>() {
                @Override
                public void success(BingElevationResponse bingElevationResponse, Response response) {
                    double elavation = bingElevationResponse.getResourceSets().get(0)
                            .getResources().get(0).getOffsets().get(0);
                    if (elavation > 0) {
                        mLocation.setElevation(elavation);
                    } else {
                        mLocation.setElevation(0);
                    }
                    responsesCount++;
                    if (requestsCount == responsesCount) {
                        setZmanimToLocation(mLocation);
                    }
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e(TAG, error.toString(), error.fillInStackTrace());
                    mLocation.setElevation(0);
                    responsesCount++;
                    if (requestsCount == responsesCount) {
                        setZmanimToLocation(mLocation);
                    }
                }
            });

还有我的专业卫士档案

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/orel/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Proguard Configuration for Realm (http://realm.io)
# For detailed discussion see: https://groups.google.com/forum/#!topic/realm-java/umqKCc50JGU
# Additionally you need to keep your Realm Model classes as well
# For example:
# -keep class com.yourcompany.realm.** { *; }

-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-keepnames public class * extends io.realm.RealmObject
-dontwarn javax.**
-dontwarn io.realm.**

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Allow obfuscation of android.support.v7.internal.view.menu.**
# to avoid problem on Samsung 4.2.2 devices with appcompat v21
# see https://code.google.com/p/android/issues/detail?id=78377
-keep class !android.support.v7.internal.view.menu.**,android.support.** {*;}


# Crashlytics 2.+

-keep class com.crashlytics.** { *; }
-keep class com.crashlytics.android.**
-keepattributes SourceFile, LineNumberTable, *Annotation*

# If you are using custom exceptions, add this line so that custom exception types are skipped during obfuscation:
-keep public class * extends java.lang.Exception

# In App Billing
-keep class com.android.vending.billing.**

## Google Play Services 4.3.23 specific rules ##
## https://developer.android.com/google/play-services/setup.html#Proguard ##

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

## GSON 2.2.4 specific rules ##

# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

-keepattributes EnclosingMethod

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

## Joda Time 2.3

-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }

# OkHttp
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

# Retrofit 1.X

-keep class retrofit.** { *; }
-keep class retrofit.http.** { *; }
-keep class retrofit.client.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn retrofit.**
-dontwarn rx.**

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

# If in your rest service interface you use methods with Callback argument.
-keepattributes Exceptions

#support design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

# https://stackoverflow.com/questions/29679177/cardview-shadow-not-appearing-in-lollipop-after-obfuscate-with-proguard/29698051
-keep class android.support.v7.widget.RoundRectDrawable { *; }

# mixpanel
-dontwarn com.mixpanel.**

# for play services
-dontwarn org.apache.http.**
-dontwarn android.net.http.AndroidHttpClient
-dontwarn com.google.android.gms.**

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.karriapps.smartsiddur.model.response.** { *; }
-keepnames public class com.karriapps.smartsiddur.model.response.**

# debug
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

编辑:我添加了pojo类本身

public class BingElevationResponse {

    private List<ResourceSet> resourceSets;

    public List<ResourceSet> getResourceSets() {
        return resourceSets;
    }

    public class ResourceSet {
        private List<Resource> resources;

        public List<Resource> getResources() {
            return resources;
        }
    }

    public class Resource {
        private List<Integer> elevations;

        public List<Integer> getOffsets() {
            return elevations;
        }
    }
}

它的路径是

com.karriapps.smartsiddur.model.response.BingElevationResponse

我非常感谢蚂蚁的帮助
谢谢

最佳答案 如果您正在使用gson jackson或任何此类json解析器库(我认为改造在内部使用一个我不确定)您需要保持任何和所有Pojo类及其FIELDS及其名称!

编辑

将@SerializedName(如果是gson)注释添加到java pojo类字段也有帮助,因为gson不必反映并获取字段名称(其中proguard更改,因此gson将使用错误的json字段名称来解析json).此外,为了使注释工作,您还需要在proguard规则中添加-keepattributes * Annotation *.

点赞