I want to send the data from the sqlite database in android to the server and delete those data from the database that have been reached to the server successfully. I have successfully written the code to send data to the server. But could not delete the data form database. How to know which data is reached to the server.
package com.example.income;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.IntentService;
import android.content.Intent;
import android.database.Cursor;
import android.util.*;
public class Background extends IntentService
{
public Background()
{
super("This is the simple background class");
}
@Override
protected void onHandleIntent(Intent intent)
{
Log.v("message","This is simple background service");
Db db =new Db(Background.this,"simple",null,4);
Cursor c= db.getData();
if( c.moveToFirst())
{
List<Map<String, String>> contacts = new ArrayList<Map<String, String>>();
do
{
String num,dater;
int integer;
integer=c.getInt (c.getColumnIndex(Base.Identifier));
num = c.getString(c.getColumnIndex(Base.CONTACTS));
dater =c.getString(c.getColumnIndex(Base.DATE));
Map<String, String> contact = new HashMap<String, String>();
contact.put("date", dater);
contact.put("contact", num);
contact.put("id",String.valueOf(integer));
contacts.add(contact);
}
while (c.moveToNext());
try
{
sendData(contacts);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e)
{
e.printStackTrace();
}
}
}
public void sendData(List<Map<String, String>> contacts) throws ClientProtocolException, IOException, JSONException
{
Log.v("Let's C","Will it go here?");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.10.115/android.php");
Log.v("p","this");
Log.d("Contacts", Integer.toString(contacts.size()));
JSONArray array= new JSONArray(contacts);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("forward",array.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response= httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
String result =EntityUtils.toString(entity);
JSONArray obj = new JSONArray(result);
for(int i =0;i<obj.length();i++)
{
JSONObject json=obj.getJSONObject(i);
String success =json.getString("id");
Log.i("success ",success);
}
/*
Runtime runtime1 = Runtime.getRuntime();
Process proc = runtime1.exec("ping -c 8 www.google.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String input;
while((input=reader.readLine())!=null)
{
Log.i("message",input);
}*/
}
}
I want to implement this as soon as data is sent to server.