-2

大家好,我在解析这个 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)
4

2 回答 2

0

我通常使用 GSON 来解析 JSON 数据..

您可以 在 java 中引用 gson 中的解析数组

您可以从 https://code.google.com/p/google-gson/获得 GSON 库

在您的项目中添加这些 google jar 文件

要解析 JSON,请使用如下代码:

connection = (HttpURLConnection) url.openConnection();
connection.connect();
reader = new InputStreamReader(connection.getInputStream(), charset);

WebsiteResults results = new Gson().fromJson(reader, WebsiteResults.class);

WebsiteResults 是您将创建的用于解析 JSON 的类,如链接中所述。

于 2013-04-10T12:14:24.100 回答
0

阅读您的日志。

这是你的错误:

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"}]}

您弄错了 url 和 post 参数。在 new HttpPost(url) 中,你的 url 实际上就是这个长的 json 字符串。(jsonText来自调用方法)。

您的其余错误来自您没有对第一个错误采取行动的事实。您的请求未执行,is保持为空,因此之后的 NPE,结果保持为空,您的 JSONObject 对空参数不满意,结果保持为空,因此是最终的 NPE。

这是“我不关心异常”的典型案例,导致错误传播远离其起源。不要忽视异常。如果连接失败(!),请不要继续处理来自 Http 连接的结果

于 2013-04-10T12:57:03.160 回答