mercredi 29 avril 2015

Trying to connect Andriod app with SQLite to SQL Db via webservice (php)

To clarify on the problem; I'm following an old tutorial to try and learn some basic sync functionality with an Android app (here). Now I have the php webservice in place, I believe the permissions are set right, the POST in the Android code is set to the correct server IP. However when I try to synch the app to the SQL I am getting a AsyncHttpClient statusCode of 0. As far as I can tell, this means it is likely the request is timing out?

If so I'm looking for how to extend the timeout limit, if not then I am stuck and would appreciate any input from someone with an idea of where I'm going wrong. The code below is the relevant part from 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://ift.tt/1DJOf1U",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();
    }

Aucun commentaire:

Enregistrer un commentaire