Steve Kamau Steve Kamau - 1 year ago 146
JSON Question

How to send SQLite data from android to MySQL server

So i am trying to send my SQLIte data with online MYSQL server but to no avail.Naturally,i ran to google and was lucky enough to find this. Apparently it's supposed to work and it does but i am not receiving the data on my server.

And yes, i know this question has been asked here and here, but i havent been able to patch it up using the suggestions given.

So here is what i have tried.This is how i convert my SQLite data into JSON using GSON:

public String composeJSONfromSQLite() {
ArrayList<HashMap<String, String>> offlineList;
offlineList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM offlineTable ";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("zip", cursor.getString(1));
map.put("phone", cursor.getString(2));
map.put("uid", cursor.getString(3));

} while (cursor.moveToNext());
Gson gson = new GsonBuilder().create();
//Use GSON to serialize Array List to JSON
return gson.toJson(offlineList);

And this is how i send it to my server:

public void syncSQLiteMySQLDB() {

AsyncHttpClient client = new AsyncHttpClient();

RequestParams params = new RequestParams();

Log.d("offline data log", loadCheckoutDB.composeJSONfromSQLite());
client.addHeader("session_id", getapikey());
client.addHeader("Content-Type", "application/json");"", params, new AsyncHttpResponseHandler() {

public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String s = new String(responseBody);

Log.d("response to sync", s);

try {

JSONObject obj = new JSONObject(s);

if (obj.getBoolean("success")) {

String success = obj.getString("message");
//Toast.makeText(getApplicationContext(), success, Toast.LENGTH_LONG).show();

} else {

String failure = obj.getString("message");
//Toast.makeText(getApplicationContext(), failure, Toast.LENGTH_LONG).show();

} catch (JSONException e) {




public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

// Toast.makeText(getApplicationContext(), "Failed to sync with server", Toast.LENGTH_LONG).show();
Log.d("sqlite sync error", String.valueOf(error));



And when i log what the json looks like from android i get this format:

"zip": "325,
"phone": "78291849",
"uid": "14538177211"

But on my server i still get an empty array. What am i doing wrong?

This is how my request format should look like:

"zip": "325,
"phone": "78291849",
"uid": "14538177211"

As requested here is how i receive the request:

public function massData()
// offline sync

$input = Input::all();

return $input;

Answer Source

Maybe there's someone who will stumble upon this question seeking help. Here's how i have done it:

 public void syncSQLiteMySQLDB() {
   //i get my json string from sqlite, see the code i posted above about this
        final String json = loadCheckoutDB.composeJSONfromSQLite();
        new Thread() {
            public void run() {
                makeRequest("http://myexample/offline/api", json);


    public void makeRequest(String uri, String json) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new StringEntity(json));
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("session_id", getapikey());
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse response = client.execute(httpPost);
            if (response != null) {

                String responseBody = EntityUtils.toString(response.getEntity());
                Log.d("response to sync", responseBody);
                Object jsonObj = new JSONTokener(responseBody).nextValue();
                if (jsonObj instanceof JSONObject) {
                    JSONObject jsonObject = (JSONObject) jsonObj;
                    //further actions on jsonObjects

                } else if (jsonObj instanceof JSONArray) {
                    //further actions on jsonArray
                    JSONArray jsonArray = (JSONArray) jsonObj;
        } catch (UnsupportedEncodingException e) {
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        } catch (JSONException e) {

From logging numerous responses i discovered that i was not sending content-type using my previous method. After trying out this code, it worked. Thanks everyone who came up with various suggestions that pushed me to log everything.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download