gpt4 book ai didi

安卓 : Synchronous way to use asyncttask : Json upload issue

转载 作者:行者123 更新时间:2023-11-29 01:29:26 24 4
gpt4 key购买 nike

更新
在下方查看我的回答

我正在尝试以 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());
}
}
}

最佳答案

As per the docs here ,可以通过使用“get()”方法同步使用 AsyncTask 类。

一个简单的例子:

MyAsyncTask task = new MyAsyncTaskTask();
String result = task.execute().get();

class MyAsyncTask extends AsyncTask<Void,Void,String>{...}

根据其中一条评论,Volley 还具有异步运行的能力。

关于安卓 : Synchronous way to use asyncttask : Json upload issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com