空指针异常json解析器

时间:2013-03-28 18:12:59

标签: android json

这就是我的尝试,

JSONObject jsonObject;
    JSONParser jParser = new JSONParser();
    jsonObject = jParser.getJSONFromUrl("http://shoppersays.com/mypromocheck/api/v1/users?&username=avinash&password=avinash2");
    try {
        String Authen_status = jsonObject.getString("Authen_status");
        Toast.makeText(getApplicationContext(), Authen_status, Toast.LENGTH_SHORT).show();
    } catch (JSONException e) { 
        Toast.makeText(getApplicationContext(), "Unable to connect", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

但是我得到了NullPointerException。我在Android 2.2上运行它,它运行成功,然后尝试4.0,它在getstring()行给出空指针异常,请帮助!!

编辑1:

日志

03-28 23:46:52.641: E/AndroidRuntime(1694): FATAL EXCEPTION: main
03-28 23:46:52.641: E/AndroidRuntime(1694): java.lang.RuntimeException: Unable to start              activity ComponentInfo{com.viralmo.readjson/com.viralmo.readjson.ReadJson}:    java.lang.NullPointerException
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.os.Looper.loop(Looper.java:137)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at java.lang.reflect.Method.invoke(Method.java:511)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at dalvik.system.NativeStart.main(Native Method)
03-28 23:46:52.641: E/AndroidRuntime(1694): Caused by: java.lang.NullPointerException
03-28 23:46:52.641: E/AndroidRuntime(1694):     at com.viralmo.readjson.ReadJson.onCreate(ReadJson.java:23)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.Activity.performCreate(Activity.java:4465)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-28 23:46:52.641: E/AndroidRuntime(1694):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-28 23:46:52.641: E/AndroidRuntime(1694):     ... 11 more

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

是的,因为@RaghavSood评论..androdi 3.x或更高版本,不允许在UI线程上进行网络操作,但是较低版本将允许。

因此,当您尝试执行网络操作时,在3.x以上版本上,您将获得NetworkOnMainThread异常。

 jsonObject = jParser.getJSONFromUrl("http://shoppersays.com/mypromocheck/api/v1/users?&username=avinash&password=avinash2");

如果您未能从服务获得响应,那么jsonObject将为null。因此对jsonObject的操作为null将抛出NullPointerException

  

如何克服它?

使用单独的线程或AsyncTask执行长时间运行