我有一些定时器操作中的 ksoap Web 服务,如果我做得好的话,它每半小时工作一次。其中一个将整个表行发送到 sql server,它把我变成 0 或 1 取决于正确发送。如果它返回 1,它必须从 sqlite 中删除所有行。尽管它返回 1,但它不会进入 if 语句,也无法使用 delete 命令。我不知道我的计时器和线程是正确的方法,我怎样才能正确地做到这一点?
编码:
try
{
final Timer V_Timer;
final Handler V_Handler;
V_Timer = new Timer();
V_Handler = new Handler(Looper.getMainLooper());
V_Timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
V_Handler.post(new Runnable()
{
public void run()
{
//for status update//
if(isConnected()){
Thread networkThread = new Thread() {
@Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_STATUS);
request.addProperty("MH_ID",hardwareID);
request.addProperty("Latitude",V_Latitude);
request.addProperty("Longitude",V_Longitude);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION_STATUS, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str_ = response.toString();
//final String str = response.toString()+"\n"+ V_Latitude +"\n"+V_Longitude;
runOnUiThread (new Runnable(){
public void run() {
Toast.makeText(MainActivity.this, str_, Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
//Insert Bulk//
if(isConnected()){
Thread networkThread2 = new Thread() {
@Override
public void run() {
str = "0";
try {
StringBuilder bulk = GetTotalRecord();
bulkinsert = bulk.toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_BULK);
request.addProperty("Insert_Bulk",bulkinsert);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION_BULK, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
str = response.toString();
runOnUiThread (new Runnable(){
public void run() {
// Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
/////delete sqlite table/////
Log.d("str", str);
if (str=="1")
{
try {
dbobject.delete(SQLiteDB.TABLE_NAME_S, null, null);
Toast.makeText(MainActivity.this, "All Answers sent", Toast.LENGTH_LONG).show() ;
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "Error: Answers could not send \n "+e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
finally{
sqlitedb.close();
}
}
/////delete sqlite table/////
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread2.start();
//Insert Bulk end//
}
}
}});
}
},10000, 1000*60*30);
}
catch(Exception ex)
{
}