使用Android应用程序将Sqlite数据与MySql数据库同步

时间:2014-08-20 04:41:56

标签: android

我开发了一个Hangman Web和移动应用程序,Iam将播放器的分数存储在用于移动应用程序的Web应用程序中的mysql数据库的表中,以便将得分存储在本地sqlite数据库中。现在我的要求是同步这两个分数。是否可能,我应该使用Web服务吗? 请帮忙解决这个问题

1 个答案:

答案 0 :(得分:0)

使用此代码从Mysql数据库中检索数据

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());
            }
        });
    }

使用此选项在SQLite中插入检索数据

 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();

        }
    }
相关问题