我想将数据从android中的sqlite数据库发送到服务器,并从数据库中删除已成功到达服务器的数据。我已成功编写代码以将数据发送到服务器。但无法删除数据表单数据库。如何知道到达服务器的数据。
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);
}*/
}
}
我想在数据发送到服务器后立即实现。
答案 0 :(得分:0)
创建一个类,用于比较通过JSON对象/数组发送到服务器的sqlite数据的值,并返回一个标志。如果成功则删除本地数据,如果不是,请尝试重新发送数据。
此链接可帮助您实现目标。
how to send data to server from android when no internet is available