With this code I send information from my local sqlite database to my server(mysql) and it works fine. But I only send data from the table1. If I want to send the data of all my sqlite tables to my server how I can do that?
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();
}
}
As you can see, I get all the data from table1 and I create a jsonObject.
- I pass that to the test.php file, but if I make another query from table2,table3…, I have to put the values of the second and third table in the same JsonObject?
- If I do that, how will I know which data is from which table?
- If I can send multiples jsonobject, how do I do that and how will I receive that data in my test.php file.
Right now I do it with $json = file_get_contents('php://input');
You already have half the solution. Just replicate the code that creates and populates a JsonObject for each additional table, and add those to your JsonArray. Instead of a JsonArray you can use another JsonObject – it can contain other JsonObjects.
On the server, just use json_decode($json, true) and you will get your data as an array of arrays – dump the result to a file with print_r() so you can see the structure of the data.