3

Is there a any way to submit data stored in an android SQLite database in format

--------------------------------------------
ID  PRODUCT_ID     AMOUNT
--------------------------------------------
1      001            4
2      008            5
3      006            2

to PHP web server using HTTP in android. I am maintaining a local database for specific transaction and after user complete the transaction I want to submit those data in to web system.Can anybody please help me regarding this.

1 Answer 1

3
  1. Convert each row of the database into a JSONObject and store all the rows into a JSONArray

  2. Convert the JSONArray into a json string using jsonArray.toString() and make an HTTP POST request. Make sure to set the Content-Type parameter to application/json

  3. On the server side, process the request accordingly and store it into a database.

Note: If you're having large amounts of data, it might be better to transfer the JSON Objects in batches. You can add parameters to the requests to denote batch ids when you reconstruct the database.

EDIT: I'm assuming you know how to fetch the cursor to the database.

You can use the following code

JSONObject jObject;
JSONArray jArray = new JSONArray()

while(cursor.moveToNext()) {

    jObject = new JSONObject();
    jObject.put("id", cursor.getInt("ID"));
    jObject.put("product_id", cursor.getString("PRODUCT_ID"));
    jObject.put("amount", cursor.getInt("AMOUNT"));
    jArray.put(jObject);

}

When you're building the HTTP POST request, do jArray.toString() to get a JSON string representing the array. Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

can you please provide me a sample code for convert each row of the database into a JSONObject using a loop.
Be sure to put these in a try/catch block as these functions throw JSONExceptions.
Let me try it and come back to reply you.
I didn't use cursor because i already fetched data from the database and stored in a List<ShoppingCartEntity> list. I simply loop the list and used your concept inside the loop. Finally it worked for me.Thank you very much and this was my first question here.
No problem.. :-). For future reference, don't store these values in a separate list when you can use the cursor directly. You might run into OutOfMemory errors. Put them into a list(or even better, a normal array) only if you need to do fast processing on those items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.