jeudi 30 juillet 2015

Parsing a list from android sqlite to remote database using a loop

Sorry for the long post - I wasn't sure how much detail to include. Bare with me please, I am fairly new to both Android and PHP.

Part of the android app I am building requires me to save a list of users in an sqlite database. The user can then enter an activity which shows the list of users currently stored in the sqlite datebase within a listview.

I have then tried to implement a function to sync this list of users to a remote database, using Async Tasks, JSON, and a PHP service, by using a FOR LOOP to cycle through the database tables 'users'.

The problem is that the sync only works for the first item in the list. After this has been passed through to the remote database, the activity crashes. The log reads:

E/JSON Parser﹕ Error parsing data org.json.JSONException: Value data of type java.lang.String cannot be converted to JSONObject

E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3 Process: example.prguru.com.pi_prototype_30, PID: 5139 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) ...

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference at example.prguru.com.pi_prototype_30.ShowAdmins$syncUser.doInBackground(ShowAdmins.java:137) at example.prguru.com.pi_prototype_30.ShowAdmins$syncUser.doInBackground(ShowAdmins.java:87)

Line 137 in the java code is:

 Log.d("Sync Attempt", jobj.toString());

PHP CODE:

require_once 'config.php';

if(!empty($_POST)){
$row = $_POST['table_id'];
$type = $_POST['type'];
$id_num = $_POST['id_number'];
$name = $_POST['name'];
$email  = $_POST['email'];
$course = $_POST['course'];
$password = $_REQUEST['password'];

$ins = mysql_query("INSERT INTO pi_prototype_inventory_30.users (table_id, type, id_number, name, email, course, password) 
VALUES('$row','$type','$id_num','$name', '$email', '$course', '$password')");

if(!$ins)
{
    echo (mysql_error());
}
else
{
    echo ("data inserted");
}
}
else {
echo (mysql_error());
}

SYNC CLASS:

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;

        DBHandler dbAdapter = DBHandler.getDBHandlerInstance(getApplicationContext());
        try {
            dbAdapter.createDataBase();
        } catch (IOException e) {
            Log.i("*** select ", e.getMessage());
        }

        dbAdapter.openDataBase();
        String query = "SELECT * FROM users";
        ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
        dbAdapter.close();

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
            for (int i = 0; i < stringList.size(); i++) {
                ArrayList<String> list = stringList.get(i);
                nameValuePairs.add(new BasicNameValuePair("table_id", list.get(0)));
                nameValuePairs.add(new BasicNameValuePair("type", list.get(1)));
                nameValuePairs.add(new BasicNameValuePair("id_number", list.get(2)));
                nameValuePairs.add(new BasicNameValuePair("name", list.get(3)));
                nameValuePairs.add(new BasicNameValuePair("email", list.get(4)));
                nameValuePairs.add(new BasicNameValuePair("course", list.get(5)));
                nameValuePairs.add(new BasicNameValuePair("password", list.get(6)));

                Log.d("Request!", "Starting!");

                jobj = jsonParser.makeObjHttpRequest(REG_URL, "POST", nameValuePairs);

                Log.d("Sync Attempt", jobj.toString());

            }//forLoop
                success = jobj.getInt(TAG_SUCCESS);
                if (success == 1){
                    Log.d("SYNCED!", jobj.toString());
                    return jobj.getString(TAG_MESSAGE);
                }
                else {
                    Log.d("Sync failure!", jobj.getString(TAG_MESSAGE));
                    return jobj.getString(TAG_MESSAGE);
                }



        } catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }//doInBackground

Do I need to implement some sort of loop in my PHP code so it accepts more than one entry from the list? I don't know how I would go about coding that because I will never be sure how many entries will need to be synced at any one time. Or is the problem within the java code itself? I'm not quite sure how to make sense of the errors presented in the log.

Aucun commentaire:

Enregistrer un commentaire