我提供了一项服务来更新我的应用程序,它应该从网上获取 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
如您所见,我有两个问题,第一个, for 应该进行 3 次迭代,但它只做一次……为什么?
第二,如果我打印出原始数据,我会得到“zodiaco”,这是我从网上获得的一个类别,但如果我将它分配给一个字符串变量,那么 println 会忽略整行......任何人的想法?:D
提前致谢。