我有一个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); –