0

アプリを更新するサービスを作成しました。ネットから JSON を取得し、データが新しい場合はデータベースに挿入します。

protected void onHandleIntent(Intent intent) {


            datasource = new pollDataSource(this);
            datasource.open();


            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(url);


            try {

                // Getting Array of Contacts
                domanda = json.getJSONArray(TAG_DOMANDE);
                System.out.println("inside the try");
                // looping through All Contacts
                System.out.println("length: " + domanda.length());
                for(int i = 0; i < domanda.length(); i++){
                    System.out.println("in the for!: " + i);
                    JSONObject c = domanda.getJSONObject(i);
                    System.out.println("raw: "+ c.getString(TAG_CATEGORIA));
                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String testoDomanda = c.getString(TAG_TESTODOMANDA);
                    String categoria = c.getString(TAG_CATEGORIA);
                    int quanteRisposte = c.getInt(TAG_QUANTERISPOSTE);
                    System.out.println("string: "+categoria);

                    datasource.createCategoria(categoria);
                    //TEOTODO inserimento della categoria, se necessario
                    //TEOTODO inserimento della domanda                 


                    for(int ua = 0; ua < quanteRisposte; ua++){
                    //TEOTODO inserimento dei testi risposte.   

                    }



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

すべての println の logcat は次のとおりです。

01-04 10:47:06.213: I/System.out(653): inside the try
01-04 10:47:06.223: I/System.out(653): length: 3
01-04 10:47:06.233: I/System.out(653): in the for!: 0
01-04 10:47:06.233: I/System.out(653): raw: zodiaco
01-04 10:47:26.773: I/System.out(653): inside the try
01-04 10:47:26.773: I/System.out(653): length: 3
01-04 10:47:26.773: I/System.out(653): in the for!: 0
01-04 10:47:26.785: I/System.out(653): raw: zodiaco
01-04 10:47:47.414: I/System.out(653): inside the try
01-04 10:47:47.423: I/System.out(653): length: 3
01-04 10:47:47.423: I/System.out(653): in the for!: 0
01-04 10:47:47.423: I/System.out(653): raw: zodiaco
01-04 10:48:07.963: I/System.out(653): inside the try
01-04 10:48:07.963: I/System.out(653): length: 3
01-04 10:48:07.963: I/System.out(653): in the for!: 0
01-04 10:48:07.973: I/System.out(653): raw: zodiaco

ご覧のとおり、2 つの問題があります。1 つ目は、for は 3 回の反復を実行する必要がありますが、実行できるのは 1 回だけです...なぜですか?

そして2番目に、生データを印刷すると、ネットから取得したカテゴリである「zodiaco」を取得しますが、それを文字列変数に割り当てると、printlnは単に行全体を無視します...アイデアとして誰か?:D

前もって感謝します。

4

1 に答える 1

0

for ループで例外が発生する可能性がありますが、空の catch ブロックで無視することにしました。

例外を処理する場合、通常、次の 2 つの状況が考えられます。

  • どうすればいいのかわからない => キャッチせず、呼び出し元のコードに処理させます (強制終了を回避したい場合は、ある段階でキャッチする必要があります)。
  • または、それを使って何か役に立つこと (再試行する、問題があることをユーザーに知らせるなど) を行うことができます。その場合は、それをキャッチして対処します。

まだ開発段階にあるため、単にログを記録するだけで十分ですが、少なくとも何が起こっているかについてのフィードバックを得ることができます。

空の catch ステートメントで例外を単に飲み込むことは、(ほとんど) 決して良い選択肢ではありません。

于 2013-01-04T11:08:00.197 に答える