Thursday, July 2, 2015

Sync Sqlite data with MySql database using android application

Use this code for retrieve data from Mysql database
public void syncSQLiteMySQLDB() {
        // Create AsycHttpClient object
        AsyncHttpClient client = new AsyncHttpClient();
        // Http Request Params Object
        RequestParams params = new RequestParams();
        client.post("Your Url", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                Log.d("Rs service_ReloadSqlDB ", response);
                updateSQLite(response);
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                Log.d(" RS onFailure ", throwable.toString());
            }
        });
    }
Use this to insert in SQLite that retrieve data
 public void updateSQLite(String response) {
        try {
            // Extract JSON array from the response
            JSONArray arr = new JSONArray(response);
            System.out.println(arr.length());
            // If no of array elements is not zero
            if (arr.length() != 0) {
                // Loop through each array element, get JSON object which has userid and username
                for (int i = 0; i < arr.length(); i++) {
                    // Get JSON object
                    JSONObject obj = (JSONObject) arr.get(i);
                    String st = obj.get("Games_Name").toString();
                    String ste = obj.get("Games_PackageName").toString();

                    // Insert User into SQLite DB
                    //   databaseHelper.insertUser(queryValues);
                    databaseHelper.insertGameInfo(st, ste);
                    Log.i(getClass().getSimpleName(), " service_ReloadSqlDB...UPDated....  Work .....");
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }

No comments:

Post a Comment