使用ProGuard时出现JavaNullPointerException

时间:2016-08-16 21:33:53

标签: java android

我有这个代码来解析来自Internet的JSON文件:

class getOnlineFeesInfo extends AsyncTask<URL, Integer, Long> {

public String feesJsonString;
protected Long doInBackground(URL... urls) {
    String language = Locale.getDefault().getLanguage();
    long state = 0;

    HttpURLConnection c = null;
    try {
        String url = "http://www.myweb.com/api/fees_" + language + ".json";
        URL u = new URL(url);
        c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);
        c.setConnectTimeout(100000);
        c.setReadTimeout(100000);
        c.connect();
        int status = c.getResponseCode();

        switch (status) {
            case 200:
            case 201:
                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                feesJsonString = sb.toString();

 try {

            System.out.println("Reading JSON from a file");
            System.out.println("----------------------------");

            JsonReader reader = new JsonReader(new StringReader(feesJsonString));

            Gson gson = new GsonBuilder().create();
            feesJson parser = gson.fromJson(reader, feesJson.class);
            parser.setData();

            for (String[] miniRecargasArray : Global.recargasArray) {
                System.out.println(miniRecargasArray[0]);
            }

            for (String[] miniOficinasArray : Global.oficinasArray) {
                System.out.println(miniOficinasArray[0]);
            }
            reader.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

           }

    } catch (MalformedURLException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (c != null) {
            try {
                c.disconnect();
            } catch (Exception ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }
   } 
    state = 1;
    return state;
    }
}

我看不出任何问题......只有在启用了ProGuard并且在运行时(发布版本变化)时它才会崩溃,如果我在调试模式下运行它就不会这样做。你能帮忙搞清楚为什么吗?谢谢!

**编辑:添加了堆栈跟踪和proguard-rules.pro **

Proguaard-rules.pro似乎不起作用,因为我在堆栈跟踪中有相同的混淆类:

    # Add project specific ProGuard rules here.
    ##---------------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.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

堆栈追踪:

08-17 01:37:31.974 31900-32075/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                   Process: com.packageName.app
                                                   java.lang.RuntimeException: An error occured while executing doInBackground()
                                                       at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                       at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                       at java.lang.Thread.run(Thread.java:841)
                                                    Caused by: java.lang.NullPointerException
                                                       at com.packageName.app.ab.a(Unknown Source)
                                                       at com.packageName.app.ac.a(Unknown Source)
                                                       at com.packageName.app.ac.doInBackground(Unknown Source)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                       at java.lang.Thread.run(Thread.java:841) 

1 个答案:

答案 0 :(得分:0)

您似乎需要为gson配置proguard。看看这个示例配置: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg