Android POSTs sqlite data and stores the data in the sent data, but even if an error occurs while fiddling with the recipient's PHP, it is considered to have been communicated and stored in the data is stored in the sent data.(The data will then be erased)
How can I stop processing data storage if there is an error on the PHP side?
Please let me know.
public class UploadAsyncTask
extensions AsyncTask <String, Integer, Integer > {
private Activity MainActivity;
public UploadAsyncTask (Activity activity) {
MainActivity=activity;
}
@ Override
protected Integer doInBackground (String...params) {
try{
HttpClient httpClient=newDefaultHttpClient();
HttpPost httpPost = new HttpPost (params[0]);
List<NameValuePair>params2 = new ArrayList<NameValuePair>();;
helper = new DBOpenHelper(MainActivity.this); // Create DB
// data acquisition
db = helper.getReadableDatabase();
db4 = helper.getWritableDatabase();
// Defining Column Names
String [ ] columns = {"productid", "name", "val1", "val2", "val3", "val4", "val5"}
// data acquisition
Cursor cursor=db.query(DB_TABLE, null, null, null, null, null, "productid");
inti = 0;
String nippoda;
db4.beginTransaction();
while(cursor.moveToNext()){
ContentValues val = new ContentValues();
val.put("productid", cursor.getString(0));
val.put("name", cursor.getString(1));
val.put("val1", cursor.getString(2));
val.put("val2", cursor.getString(3));
val.put("val3", cursor.getString(4));
val.put("val4", cursor.getString(5));
val.put("val5", cursor.getString(6));
// data update
db4.insert(DB_TABLE4, null, val);
data=cursor.getString(0)+"\t"+cursor.getString(1)+"\t"+cursor.getString(2)+"\t"+cursor.getString(3)+"\t"+cursor.getString(4)+"\t"+cursor.getString(5)+"\t"+cursor.getString(5);6
param2.add(new BasicNameValuePair("product["+i+"]", data));
i++;
}
SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences (MainActivity.this);
String Key = sp.getString("SaveString", null);
// Situation code
param2.add(new BasicNameValuePair("key", Key));
db4.setTransactionSuccessful();
// end of transaction control
db4.endTransaction();
try{
httpPost.setEntity(new UrlEncodedFormEntity(params2, "UTF-8"));
HttpResponse httpResponse=httpClient.execute(httpPost);
// Get status code
int statusCode=httpResponse.getStatusLine().getStatusCode();
Log.e("statusCode", String.valueOf(statusCode)));
// Get Response
HttpEntity entity = httpResponse.getEntity();
String response=EntityUtils.toString(entity);
entity.consumeContent();
// terminate a client
httpClient.getConnectionManager().shutdown();
db.beginTransaction();
db.execSQL ("DELETE FROM" + DB_TABLE);
db.setTransactionSuccessful();
// end of transaction control
db.endTransaction();
} catch(UnsupportedEncodingException e1){
e1.printStackTrace();
}
} catch(ClientProtocolExceptione){
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
return 0;
}
@ Override
protected void onPostExecute (Integer result) {
if(result==null){
Toast.makeText(NP_Submit.this, "Error", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(NP_Submit.this, "ok", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
If you look at the source code, immediately after registering to the database,
db4.setTransactionSuccessful();
db4.endTransaction();
and the transaction was committed immediately, so
Database registration → HTTP communication → Database deletion Commit transaction after everything is complete.
If an exception is detected, you may want to roll back the transaction.
Perhaps there is a method in db4 that indicates a transaction failure (?), so try setting it up and endTransaction().
Please forgive me if there is a mistake.
© 2024 OneMinuteCode. All rights reserved.