我有一个Android应用程序,我通过json上传一些数据。我在一个单独的线程中执行此过程。
问题:有没有办法知道此线程何时成功结束。所以我可以打电话给所有发送的数据。
代码
protected void sendJson(){
if(isOnline()){
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); //Timeout Limit
HttpResponse response;
// JSONObject json = new JSONObject();
try {
String URL ="http://myservice.azurewebsites.net/Service/RegisterStudent";
HttpPost post = new HttpPost(URL);
JSONObject StudentData = new JSONObject();
String android_id = Secure.getString(AdminSection.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);
// Toast.makeText(getBaseContext(), finaldata.toString(), Toast.LENGTH_LONG).show();
StringEntity se = new StringEntity( finaldata.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
String jsonString = EntityUtils.toString(response.getEntity());
JSONObject resp = null;
resp = new JSONObject(jsonString);
JSONObject UploadStudentDataResult = resp.getJSONObject("RegisterStudentResult");
String strMessage = UploadStudentDataResult.getString("IsUploaded");
// Toast.makeText(getBaseContext(), strMessage, Toast.LENGTH_LONG).show();
if (StudentIDForSave != null){
SQLiteDatabase db1;
ContentValues values = new ContentValues();
values.put(DBHelper.isUploaded, strMessage);
// Call update method of SQLiteDatabase Class and close after
// performing task
db1 = helper.getWritableDatabase();
db1.update(DBHelper.TABLEStudent, values, DBHelper.S_ID + "=?",
new String[] { StudentIDForSave});
db1.close();
}
}
// tvSetCount.setText("Data Uploaded Successfully");
}catch(Exception e) {
e.printStackTrace();
//createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
}
答案 0 :(得分:0)
findViewById()