0

这是我的代码。在这个我检索存储为共享首选项中的字符串的 json 数据。但相反,我得到了下面的异常。现在问题是我已经尝试打印出“ String fav” 3 次,但它只是忽略了它Log.i并且不打印它。所以我不知道是什么问题..请帮帮我.. thnx提前

public class RetriveData {  

static public void savefavRetailer() {

    JSONArray jArray = new JSONArray();
    if (Perma.favRetailer.isEmpty()) {
        jArray.put(Perma.followerId);
    } else {
        try {
            for (int i = 0; i < Perma.favRetailer.size(); i++) {
                jArray.put(Perma.favRetailer.get(i));
            }
            jArray.put(Perma.favRetailer.size(), Perma.followerId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    Perma.editor.putString("favRetailer", jArray.toString());
    Perma.editor.putString("favRetailer " + Perma.followerId, Perma.followerName);
    Perma.editor.commit();
}

static public void removefavRetailer() {
    Perma.favRetailer.remove(Perma.followerId);
    JSONArray jArray = new JSONArray(Perma.favRetailer);
    Perma.editor.putString("favRetailer", jArray.toString());
    Perma.editor.commit();
}




static ArrayList<String> getfavRetailer() {
    String fav ;
    Log.i("I getArrayofRetailer", "In");
    Log.i("I getArrayofRetailer", "In");
    fav= Perma.sharedPref.getString("favRetailer", "");

    Log.i("REtrive Data fav",""+fav);
    if (fav != null) {
        Log.i("REtrive Data 2ndfav",""+fav);    
        try {
            Log.i("REtrive Data 3rdfav",""+fav);
            JSONArray jArray = new JSONArray(fav);
            for (int i = 0; i < jArray.length(); i++) {
                String temp = jArray.optString(i);
                Perma.favRetailer.add(temp);
            }
            Log.i("I getArrayofRetailer", "jArray " + jArray.toString());
        } catch (JSONException e) {
            Log.i("getArrayofRetailer", "Exception");
            e.printStackTrace();
        }
    }
    return Perma.favRetailer;
}

}

这里是日志。现在在这我只得到三倍的日志???

04-29 11:11:33.165: I/I getArrayofRetailer(882): In
04-29 11:11:33.165: I/I getArrayofRetailer(882): In
04-29 11:11:33.176: I/getArrayofRetailer(882): Exception

04-29 11:11:33.184: W/System.err(882): org.json.JSONException: End of input at  character 0 of 
04-29 11:11:33.184: W/System.err(882):  at    org.json.JSONTokener.syntaxError(JSONTokener.java:446)
04-29 11:11:33.194: W/System.err(882):  at org.json.JSONTokener.nextValue(JSONTokener.java:93)
04-29 11:11:33.194: W/System.err(882):  at org.json.JSONArray.<init>(JSONArray.java:87)
04-29 11:11:33.194: W/System.err(882):  at org.json.JSONArray.<init>(JSONArray.java:103)
04-29 11:11:33.194: W/System.err(882):  at rensoft.connectyfinal.RetriveData.getfavRetailer(RetriveData.java:34)
04-29 11:11:33.194: W/System.err(882):  at rensoft.connectyfinal.Inbox.onCreate(Inbox.java:52)
04-29 11:11:33.194: W/System.err(882):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 11:11:33.214: W/System.err(882):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-29 11:11:33.214: W/System.err(882):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-29 11:11:33.214: W/System.err(882):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-29 11:11:33.214: W/System.err(882):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-29 11:11:33.214: W/System.err(882):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 11:11:33.214: W/System.err(882):  at android.os.Looper.loop(Looper.java:123)
04-29 11:11:33.224: W/System.err(882):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-29 11:11:33.224: W/System.err(882):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 11:11:33.224: W/System.err(882):  at java.lang.reflect.Method.invoke(Method.java:507)
04-29 11:11:33.224: W/System.err(882):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-29 11:11:33.224: W/System.err(882):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-29 11:11:33.234: W/System.err(882):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

将行更改if (fav != null) {if (fav.length() == 0) {

fav= Perma.sharedPref.getString("favRetailer", "");如果“favRetailer”项在您的 SharedPreferences 中不存在,则返回一个空字符串,因此您必须在解析 json 之前检查一个空字符串而不是 null。

于 2013-04-30T20:23:20.577 回答