我试图在我的Android应用程序中解析JSON并使用本机java解析器。我已遵循此tutorial,我的代码如下。
@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;
}
我完全遵循了教程,应用程序一次又一次地崩溃。 Log cat如下:
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
答案 0 :(得分: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;
}
你会看到发生了什么。吞没一个没有痕迹的异常几乎总是一个坏主意。
答案 1 :(得分: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 无效,因为那是循环结束时的情况。显然,返回语句将因NPE而失败。您可以尝试以下方式。
String inputLine;
StringBuilder builder = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
builder.append(inputLine);
}
in.close();
return builder.substring(14).toString();