尝试通过webservice(php)将Android应用程序与SQLite连接到SQL Db

时间:2015-04-29 11:52:18

标签: php android sqlite synchronization

澄清问题;我正在按照旧教程尝试使用Android应用(here)学习一些基本的同步功能。现在我已经安装了php webservice,我相信权限设置正确,Android代码中的POST设置为正确的服务器IP。但是,当我尝试将应用程序同步到SQL时,我得到AsyncHttpClient statusCode为0.据我所知,这意味着请求可能超时了吗?

如果是这样,我正在寻找如何延长超时限制,如果没有,那么我会被卡住,并会感谢有人知道我哪里出错了。以下代码是Android Studio的相关部分。

public void syncSQLiteMySQLDB(){
    //Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    ArrayList<HashMap<String, String>> userList =  controller.getAllUsers();
    if(userList.size()!=0){
        if(controller.dbSyncCount() != 0){
            prgDialog.show();
            params.put("usersJSON", controller.composeJSONfromSQLite());
            client.post("http://46.105.118.169:9000/sqlitemysqlsync/insertuser.php",params ,new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    System.out.println(response);
                    prgDialog.hide();
                    try {
                        JSONArray arr = new JSONArray(response);
                        System.out.println(arr.length());
                        for(int i=0; i<arr.length();i++){
                            JSONObject obj = (JSONObject)arr.get(i);
                            System.out.println(obj.get("id"));
                            System.out.println(obj.get("status"));
                            controller.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString());
                        }
                        Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Throwable error,
                                      String content) {
                    // TODO Auto-generated method stub
                    prgDialog.hide();
                    if(statusCode == 404){
                        Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }else if(statusCode == 500){
                        Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Error: " + statusCode, Toast.LENGTH_LONG).show();

                    }
                }
            });
        }else{
            Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
    }

0 个答案:

没有答案