-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

ログを読みます。

これはあなたのエラーです:

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 と投稿パラメータを間違えています。new HttpPost(url) では、URL は実際にはこの長い json 文字列です。(jsonText呼び出しメソッドから)。

残りのエラーは、最初のエラーに対処しなかったことに起因します。リクエストは実行されず、isnull のままになるため、その後の NPE は空のままになり、JSONObject は空のパラメーターに不満を持ち、結果は null のままになるため、最終的な NPE になります。

これは「例外は気にしない」の典型的なケースであり、エラーが発生源から遠くまで伝播します。例外を無視しないでください。接続が失敗した場合、HTTP 接続からの結果に対して処理を続行しないでください (!)

于 2013-04-10T12:57:03.160 に答える
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 に答える