【发布时间】:2015-08-18 09:45:54
【问题描述】:
更新
在下方查看我的答案
我正在尝试以 json 格式发送多个学生数据及其响应。
问题
当表格中有多行时。该函数一次发出多个请求。然后我从回调函数得到多个响应......我想要一个请求及其回调。然后提出新的请求
我想要这个
final ConnectToServer connect = new ConnectToServer();
connect.extConnectToServer(AdminSection.this,new ConnectToServer.Callback()
每个请求发出后调用的函数。完成整个循环后调用回调函数。
代码
public void Uploadalldata()
{
if(isOnline())
{
JSONObject StudentData = new JSONObject();
try
{
String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
DBHelper db = new DBHelper(getApplicationContext());
List<StudentClass> StudentDataAll = db.getAllStudentData();
for(int iCount=0; iCount< StudentDataAll.size(); iCount++)
{
StudentClass objStudentClass= (StudentClass)StudentDataAll.get(iCount);
String sSingleStudentCompleteDetails= android_id +","+ objStudentClass.RegistrationId + "," + objStudentClass.Name + "," + objStudentClass.SchoolID + "," + objStudentClass.Class + "," + objStudentClass.RollNo + "," + objStudentClass.RegistrationDate;
String sSingleStudentCompleteResponse = "";
String strStudentID = objStudentClass.RegistrationId;
StudentIDForSave = strStudentID;
List<StudentResponse> StudentResponse = db.getStudentResponseOnStudentID(strStudentID);
for(int iOptionCount=0; iOptionCount<StudentResponse.size(); iOptionCount++)
{
StudentResponse objStudentResponse=StudentResponse.get(iOptionCount);
if(iOptionCount>0)
sSingleStudentCompleteResponse += ",";
sSingleStudentCompleteResponse += objStudentResponse.QuestionID + "-" + objStudentResponse.OptionID;
}
StudentData.put("StudentDetails", sSingleStudentCompleteDetails);
StudentData.put("Responses", sSingleStudentCompleteResponse);
JSONObject finaldata = new JSONObject();
finaldata.put("RegisterStudentRequest", StudentData);
final ConnectToServer connect = new ConnectToServer();
connect.extConnectToServer(AdminSection.this,new ConnectToServer.Callback()
{
public void callFinished(String result)
{
JSONObject resp = null;
try
{
resp = new JSONObject(result);
JSONObject UploadStudentDataResult = resp.getJSONObject("RegisterStudentResult");
String strMessage = UploadStudentDataResult.getString("IsUploaded");
if (StudentIDForSave != null)
{
SQLiteDatabase db;
ContentValues values = new ContentValues();
values.put(DBHelper.isUploaded, strMessage);
// Call update method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.update(DBHelper.TABLEStudent, values, DBHelper.S_ID + "=?",
new String[] { StudentIDForSave});
db.close();
//Toast.makeText(getBaseContext(), "saved", Toast.LENGTH_LONG).show();
}
// else
// {
// Toast.makeText(getBaseContext(), "Data not saved", Toast.LENGTH_LONG).show();
// }
}
catch (final JSONException e)
{
}
}
}, "http://myurl/Service/RegisterStudent", finaldata, "POST");
connect.execute(finaldata).get();
}
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
}
}
【问题讨论】:
-
如果您收到多行作为 JSON 响应,那么为什么不在 JSON 数组中发送响应? JSON 数组可以有多个 JSON 对象,并且可以在一个请求中获取。 P.S 尝试使用 Volley 库连接到服务器并获得响应。
-
对 android 比较陌生,已经做了 3 个月的 android,信息链接会有所帮助。只是一个示例或演示或文档
-
如果你卡在某个地方,请在 facebook 上联系我,dpkramrakhyani@gmail.com
-
好的...一定会的
标签: android json rest httpclient