アプリを更新するサービスを作成しました。ネットから 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
前もって感謝します。