我有一個ListView
超過40項,我想保存他們的詳細信息在MySQL。對於前面的4-5項,所有效果都很好,然後循環停止而沒有任何錯誤。當我試圖把它們放在一個數組,然後立刻把他們所有的JSONAndroid調用php for for循環
String student,obtained_value,max_value;
for (int i=0;i<listStudent.getChildCount();i++) {
View view=listStudent.getChildAt(i);
studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls);
obtainedTxt=(EditText) view.findViewById(R.id.obtained);
maxTxt=(TextView) view.findViewById(R.id.max);
student=studentIdTxt.getText().toString();
obtained_value=obtainedTxt.getText().toString();
max_value=maxTxt.getText().toString();
//updating the new mark list array
/*HashMap<String,String>studentMark=new HashMap<String,String>();
studentMark.put(TAG_STUDENT_ID,student);
studentMark.put(TAG_MARKS_OBTAINED,obtained_value);
studentMark.put(TAG_MARKS_MAX, max_value);*/
//studentMarksList.add(studentMark);
//transform studentMarksList to json
//String studentList=gson.toJson(studentMarksList);
String URL_CHECK=domain+"/app/caller.php?action=save_marks&work_id="+workId+"&student="+student+"&obtained="+obtained_value+"&max="+max_value+"&course="+course+"&staff="+staff;
String response=caller.makeServiceCall(URL_CHECK,serviceHandler.POST);
boolean result=false;
Log.d("Response: ", "> " + response);
if (response != null) {
try {
JSONObject jsonObj = new JSONObject(response);
// Getting JSON Array node
ServerReply = jsonObj.getJSONArray(TAG_CHECK_RESULT);
JSONObject c=ServerReply.getJSONObject(0);
String error_txt= c.getString(TAG_ERROR_TXT);
String error_code=c.getString(TAG_ERROR_CODE);
String message=c.getString(TAG_MESSAGE);
if(error_txt.equals("success")){
result=true;
}else{
result=false;
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
註釋行被使用,但它並沒有奏效。
如何在不停止循環的情況下保存ListView
中的所有項目?
嘗試一次發送全部。截至目前如果您有20個項目,現在您將有20個webservice調用。還可以使用「AsyncTask」進行web服務調用。不要在主線程上運行長時間運行的操作。 –
其實這些代碼是在doInBackground中調用的方法,我試圖一次發送它們,但傳輸JSON一直是個問題。我將它們放入Hasmap並將其轉換爲JSON,但將其作爲參數發送給我,讓我很頭疼。 –
把所有的字符串數組,然後將其轉換爲字符串由此--->字符串user_id = Arrays.toString(item); –