大家好,我在解析这个 JSON 文件时遇到问题http://ammantimes.net/android/ammantimes.php?at_mod=news 请帮我解决问题:
这是我用来解析它的代码:
public String getJSONTextFromUrl(String url) {
// initialize
InputStream is = null;
String result = "";
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = URLDecoder.decode(sb.toString(), "UTF-8");
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
System.out.println("JSON: = " + result);
System.out.println("JSON SIZE: = " + result.length());
// return result.replace(result.charAt(0), ' ');
return result;
}
public JSONObject getJSONFromScript(String jsonText) {
JSONObject jObject = null;
String result = getJSONTextFromUrl(jsonText);
// try parse the string to a JSON object
try {
jObject = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
e.printStackTrace();
}
return jObject;
}
这是我得到的 logcat 输出和错误:
04-10 14:48:25.070: I/System.out(28470): JSON: = {"news":[{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"}]}
04-10 14:48:25.070: I/System.out(28470): JSON SIZE: = 755
04-10 14:48:25.070: I/APACHE HTTP (thCr=11) - NafHttpAuthStrategyDefault(28470): (thUse=11) NafHttpAuthStrategyDefault()
04-10 14:48:25.070: E/log_tag(28470): Error in http connection java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {"news":[{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"},{"NewsID":"aa","StatusID":"aa","NewsSubject":"aa","NewsTitle":"aa","NewsPhoto":"aa","IsDeleted":"aa","SubmitDateTime":"aa"}]}
04-10 14:48:25.070: E/log_tag(28470): Error converting result java.lang.NullPointerException
04-10 14:48:25.070: I/System.out(28470): JSON Text: =
04-10 14:48:25.070: I/System.out(28470): JSON Text SIZE: = 0
04-10 14:48:25.070: E/log_tag(28470): Error parsing data org.json.JSONException: End of input at character 0 of
04-10 14:48:25.070: W/dalvikvm(28470): threadid=9: thread exiting with uncaught exception (group=0x4001e578)
04-10 14:48:25.070: E/AndroidRuntime(28470): FATAL EXCEPTION: Thread-11
04-10 14:48:25.070: E/AndroidRuntime(28470): java.lang.NullPointerException
04-10 14:48:25.070: E/AndroidRuntime(28470): at com.jordaneaucation.json.JSONParser.getNewsMap(JSONParser.java:59)
04-10 14:48:25.070: E/AndroidRuntime(28470): at com.jordaneaucation.ui.NewsTab$1.run(NewsTab.java:72)
04-10 14:48:25.070: E/AndroidRuntime(28470): at java.lang.Thread.run(Thread.java:1019)
04-10 14:48:31.570: E/WindowManager(28470): Activity com.jordaneaucation.ui.NewsTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40530778 that was originally added here
04-10 14:48:31.570: E/WindowManager(28470): android.view.WindowLeaked: Activity com.jordaneaucation.ui.NewsTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40530778 that was originally added here
04-10 14:48:31.570: E/WindowManager(28470): at android.view.ViewRoot.<init>(ViewRoot.java:273)
04-10 14:48:31.570: E/WindowManager(28470): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
04-10 14:48:31.570: E/WindowManager(28470): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
04-10 14:48:31.570: E/WindowManager(28470): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.Dialog.show(Dialog.java:241)
04-10 14:48:31.570: E/WindowManager(28470): at com.jordaneaucation.ui.NewsTab.onCreate(NewsTab.java:44)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-10 14:48:31.570: E/WindowManager(28470): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 14:48:31.570: E/WindowManager(28470): at android.os.Looper.loop(Looper.java:130)
04-10 14:48:31.570: E/WindowManager(28470): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-10 14:48:31.570: E/WindowManager(28470): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:48:31.570: E/WindowManager(28470): at java.lang.reflect.Method.invoke(Method.java:507)
04-10 14:48:31.570: E/WindowManager(28470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-10 14:48:31.570: E/WindowManager(28470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-10 14:48:31.570: E/WindowManager(28470): at dalvik.system.NativeStart.main(Native Method)
这是 printStackTrace()
04-10 15:09:58.189: W/System.err(28947): org.json.JSONException: End of input at character 0 of
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONTokener.nextValue(JSONTokener.java:93)
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONObject.<init>(JSONObject.java:154)
04-10 15:09:58.189: W/System.err(28947): at org.json.JSONObject.<init>(JSONObject.java:171)
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.json.JSONParser.getJSONFromScript(JSONParser.java:131)
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.json.JSONParser.getNewsMap(JSONParser.java:54)
04-10 15:09:58.189: W/System.err(28947): at com.jordaneaucation.ui.NewsTab$1.run(NewsTab.java:72)
04-10 15:09:58.189: W/System.err(28947): at java.lang.Thread.run(Thread.java:1019)
04-10 15:09:58.189: W/dalvikvm(28947): threadid=9: thread exiting with uncaught exception (group=0x4001e578)