-1

我试图在我的 android 应用程序中解析 JSON,并且正在使用本机 java 解析器。我已按照本教程进行操作,我的代码如下。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    // getting JSON string from URL
    JSONObject json = getObject();

    try {
        // Getting Array of Contacts
        photos = json.getJSONObject("photos");

        JSONArray photo = photos.getJSONArray("photo");
        // looping through All Contacts
        for(int i = 0; i < photo.length(); i++){
            JSONObject c = photo.getJSONObject(i);


            String id = c.getString("id");
            String owner = c.getString("owner");
            String secret = c.getString("secret");
            String server = c.getString("server");
            int farm = c.getInt("farm");
            String title = c.getString("title");
            int ispublic = c.getInt("ispublic");
            int isfriend = c.getInt("isfriend");
            int isfamily = c.getInt("isfamily");


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







}

private String getJSon() throws IOException{


    String urlString = "http://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=APIKEY"+
            "&tags="+ "ubccpsc210" +"&lat=49.260887&lon=-123.24902&safe_search=1&per_page=20";   

    URL flickrJSon = new URL(urlString);
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        flickrJSon.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null)
        inputLine += inputLine;

    in.close();

    return inputLine.substring(14, inputLine.length());
}


private JSONObject getObject(){

    try{
        obj = new JSONObject(getJSon());
    }catch(Exception e){
        return null;
    }
    return obj;
}

我已经完全按照教程进行操作,并且应用程序一次又一次地崩溃。日志猫如下:

03-29 13:50:40.899: E/Zygote(33): setreuid() failed. errno: 2
03-29 13:50:48.378: E/Zygote(33): setreuid() failed. errno: 17
03-29 13:50:49.858: E/BatteryService(59): usbOnlinePath not found
03-29 13:50:49.858: E/BatteryService(59): batteryVoltagePath not found
03-29 13:50:49.858: E/BatteryService(59): batteryTemperaturePath not found
03-29 13:50:49.879: E/SurfaceFlinger(59): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
03-29 13:50:55.479: E/EventHub(59): could not get driver version for /dev/input/mouse0, Not a typewriter
03-29 13:50:55.479: E/EventHub(59): could not get driver version for /dev/input/mice, Not a typewriter
03-29 13:50:55.649: E/System(59): Failure starting core service
03-29 13:50:55.649: E/System(59): java.lang.SecurityException
03-29 13:50:55.649: E/System(59):   at android.os.BinderProxy.transact(Native Method)
03-29 13:50:55.649: E/System(59):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
03-29 13:50:55.649: E/System(59):   at android.os.ServiceManager.addService(ServiceManager.java:72)
03-29 13:50:55.649: E/System(59):   at com.android.server.ServerThread.run(SystemServer.java:184)
03-29 13:50:56.408: E/SoundPool(59): error loading /system/media/audio/ui/Effect_Tick.ogg
03-29 13:50:56.418: E/SoundPool(59): error loading /system/media/audio/ui/KeypressStandard.ogg
03-29 13:50:56.418: E/SoundPool(59): error loading /system/media/audio/ui/KeypressSpacebar.ogg
03-29 13:50:56.418: E/SoundPool(59): error loading /system/media/audio/ui/KeypressDelete.ogg
03-29 13:50:56.418: E/SoundPool(59): error loading /system/media/audio/ui/KeypressReturn.ogg
03-29 13:50:57.559: E/ThrottleService(59): Could not open GPS configuration file /etc/gps.conf
03-29 13:50:58.488: E/logwrapper(131): executing /system/bin/tc failed: No such file or directory
03-29 13:50:58.518: E/logwrapper(134): executing /system/bin/tc failed: No such file or directory
03-29 13:50:58.549: E/logwrapper(135): executing /system/bin/tc failed: No such file or directory
03-29 13:51:07.671: E/HierarchicalStateMachine(59): TetherMaster - unhandledMessage: msg.what=3
03-29 14:29:09.924: E/AndroidRuntime(548): FATAL EXCEPTION: main
03-29 14:29:09.924: E/AndroidRuntime(548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.jsonparser/com.test.jsonparser.MainActivity}: java.lang.NullPointerException
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.os.Looper.loop(Looper.java:123)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 14:29:09.924: E/AndroidRuntime(548):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:29:09.924: E/AndroidRuntime(548):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 14:29:09.924: E/AndroidRuntime(548):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 14:29:09.924: E/AndroidRuntime(548):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 14:29:09.924: E/AndroidRuntime(548):  at dalvik.system.NativeStart.main(Native Method)
03-29 14:29:09.924: E/AndroidRuntime(548): Caused by: java.lang.NullPointerException
03-29 14:29:09.924: E/AndroidRuntime(548):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:35)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 14:29:09.924: E/AndroidRuntime(548):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 14:29:09.924: E/AndroidRuntime(548):  ... 11 more
03-29 14:29:31.411: E/AndroidRuntime(578): FATAL EXCEPTION: main
03-29 14:29:31.411: E/AndroidRuntime(578): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.jsonparser/com.test.jsonparser.MainActivity}: java.lang.NullPointerException
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.os.Looper.loop(Looper.java:123)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 14:29:31.411: E/AndroidRuntime(578):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:29:31.411: E/AndroidRuntime(578):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 14:29:31.411: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 14:29:31.411: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 14:29:31.411: E/AndroidRuntime(578):  at dalvik.system.NativeStart.main(Native Method)
03-29 14:29:31.411: E/AndroidRuntime(578): Caused by: java.lang.NullPointerException
03-29 14:29:31.411: E/AndroidRuntime(578):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:35)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 14:29:31.411: E/AndroidRuntime(578):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 14:29:31.411: E/AndroidRuntime(578):  ... 11 more
03-29 14:33:29.061: E/AndroidRuntime(606): FATAL EXCEPTION: main
03-29 14:33:29.061: E/AndroidRuntime(606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.jsonparser/com.test.jsonparser.MainActivity}: java.lang.NullPointerException
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.os.Looper.loop(Looper.java:123)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 14:33:29.061: E/AndroidRuntime(606):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:33:29.061: E/AndroidRuntime(606):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 14:33:29.061: E/AndroidRuntime(606):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 14:33:29.061: E/AndroidRuntime(606):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 14:33:29.061: E/AndroidRuntime(606):  at dalvik.system.NativeStart.main(Native Method)
03-29 14:33:29.061: E/AndroidRuntime(606): Caused by: java.lang.NullPointerException
03-29 14:33:29.061: E/AndroidRuntime(606):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:35)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 14:33:29.061: E/AndroidRuntime(606):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 14:33:29.061: E/AndroidRuntime(606):  ... 11 more
03-29 14:33:47.161: E/AndroidRuntime(635): FATAL EXCEPTION: main
03-29 14:33:47.161: E/AndroidRuntime(635): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.jsonparser/com.test.jsonparser.MainActivity}: java.lang.NullPointerException
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.os.Looper.loop(Looper.java:123)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 14:33:47.161: E/AndroidRuntime(635):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:33:47.161: E/AndroidRuntime(635):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 14:33:47.161: E/AndroidRuntime(635):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 14:33:47.161: E/AndroidRuntime(635):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 14:33:47.161: E/AndroidRuntime(635):  at dalvik.system.NativeStart.main(Native Method)
03-29 14:33:47.161: E/AndroidRuntime(635): Caused by: java.lang.NullPointerException
03-29 14:33:47.161: E/AndroidRuntime(635):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:35)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 14:33:47.161: E/AndroidRuntime(635):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 14:33:47.161: E/AndroidRuntime(635):  ... 11 more
03-29 14:39:02.912: E/AndroidRuntime(665): FATAL EXCEPTION: main
03-29 14:39:02.912: E/AndroidRuntime(665): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.jsonparser/com.test.jsonparser.MainActivity}: java.lang.NullPointerException
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.os.Looper.loop(Looper.java:123)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 14:39:02.912: E/AndroidRuntime(665):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:39:02.912: E/AndroidRuntime(665):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 14:39:02.912: E/AndroidRuntime(665):  at dalvik.system.NativeStart.main(Native Method)
03-29 14:39:02.912: E/AndroidRuntime(665): Caused by: java.lang.NullPointerException
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.test.jsonparser.MainActivity.example(MainActivity.java:76)
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:30)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 14:39:02.912: E/AndroidRuntime(665):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 14:39:02.912: E/AndroidRuntime(665):  ... 11 more
4

2 回答 2

0

该语句e.printStackTrace();在 Android 应用程序中没有多大用处,因为您不会在 logcat 中看到它。最好将其转换为 logcat 输出

Log.d(TAG, "Exception", e);

除此之外,一种可能的异常来源IOException

return inputLine.substring(14, inputLine.length());

如果来自服务器的响应少于 14 个字符。

getObject()中,添加一个Log.d之前return null

private JSONObject getObject(){
    try {
        obj = new JSONObject(getJSon());
    } catch(Exception e) {
        Log.d(TAG, "getObject(): Exception", e);
        return null;
    }

    return obj;
}

你会看到发生了什么。吞下没有痕迹的异常几乎总是一个坏主意。

于 2013-03-29T21:59:55.897 回答
0

如您所见,有几个NullPointerException

03-29 14:29:09.924: E/AndroidRuntime(548): Caused by: java.lang.NullPointerException
03-29 14:29:09.924: E/AndroidRuntime(548):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:35)

03-29 14:39:02.912: E/AndroidRuntime(665): Caused by: java.lang.NullPointerException
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.test.jsonparser.MainActivity.example(MainActivity.java:76)
03-29 14:39:02.912: E/AndroidRuntime(665):  at com.test.jsonparser.MainActivity.onCreate(MainActivity.java:30)

确保给定行号中的语句(在MainActivity.onCreate()中)适用于非空对象。例如,确保方法getObject()返回一个非空值。

- 编辑 -

String inputLine;

while ((inputLine = in.readLine()) != null)
    inputLine += inputLine;

in.close();

return inputLine.substring(14, inputLine.length());

首先,这个 while 循环是没有用的。(1) 它覆盖了inputLine (2)中的读取行,它最终使inputLine无效,因为那是循环结束的时候。很明显,return 语句将因 NPE 而失败。您可以尝试以下方法。

String inputLine;
StringBuilder builder = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
    builder.append(inputLine);
}

in.close();

return builder.substring(14).toString();
于 2013-03-29T22:57:44.233 回答