使用此代码,我将本地 sqlite 数据库中的信息发送到我的服务器(mysql),它工作正常。但我只从 table1 发送数据。如果我想将我所有的 sqlite 表的数据发送到我的服务器,我该怎么做?
public class sendInformation {
public SQLiteDatabase newDB;
JSONObject jObject;
JSONArray jArray = new JSONArray();
public void getValues()
{
try
{
newDB = SQLiteDatabase.openDatabase("/data/data/com..../databases/...db",null,SQLiteDatabase.CONFLICT_NONE);
}catch(SQLException e)
{
Log.d("Error","Error while Opening Database");
e.printStackTrace();
}
try
{
Cursor c = newDB.rawQuery("Select * from table1",null);
if (c.getCount()==0)
{
c.close();
Log.d("","no items on the table");
}else
{
c.moveToFirst();
while(c.moveToNext()) {
jObject = new JSONObject();
jObject.put("ID", c.getString(c.getColumnIndex("ID")));
jObject.put("Notes", c.getString(c.getColumnIndex("Notes")));
jObject.put("Cellphone", c.getString(c.getColumnIndex("Cellphone")));
jObject.put("Date", "null");
jObject.put("Address", "null");
jArray.put(jObject);
}
c.close();
Log.d("","ALL the data in the DB"+jArray.toString());
int arrayLength=jArray.length();
Log.d("","Lenght of the jArray"+arrayLength);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,9000);
HttpConnectionParams.setSoTimeout(httpParams, 9000);
HttpClient client = new DefaultHttpClient(httpParams);
String url = "http://ipaddress/..../test.php?arrayLength="+arrayLength;
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(jArray.toString().getBytes("UTF8")));
request.setHeader("json", jArray.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.i("Read from server", result);
}
}
} catch (UnsupportedEncodingException uee) {
Log.d("Exceptions", "UnsupportedEncodingException");
uee.printStackTrace();
}catch (Throwable t) {
Log.d("","request fail"+t.toString());
}
this.newDB.close();
}
}
如您所见,我从中获取所有数据table1
并创建了一个 jsonObject。
- 我将它传递给 test.php 文件,但如果我从 table2、table3... 进行另一个查询,我必须将第二个和第三个表的值放在同一个 JsonObject 中?
- 如果我这样做,我怎么知道哪些数据来自哪个表?
- 如果我可以发送多个 jsonobject,我该怎么做以及如何在我的 test.php 文件中接收该数据。
现在我用$json = file_get_contents('php://input');