1

well my problem is this, I have: 1. Database in mysql with the table users 2. In the app there's a register where you put username and telephone which are stored in the table users

When you have registered, in the app there's a activity Contacts that must sync the users and phonenumbers from the database but only sync the users with the number phones that have the phone directory obvious using ContactsContract

This is my code: MainContactos.java

class LoadAllProducts extends AsyncTask<String, String, String> {

        JSONObject json;
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainContactos.this);
            pDialog.setMessage("Cargando Usuarios. Porfavor espera...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            while (cursor.moveToNext()) {
                phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("phoneNumber", phoneNumber));
            json = jsonParser.makeHttpRequest(url_all_products, "POST", params);
            // getting JSON string from URL
            json = jsonParser.makeHttpRequest(url_all_products, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("Todos los usuarios: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if(success==1){
                    // looping through All Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String telePhone = c.getString(TAG_TELEPHONE);
                        String userName = c.getString(TAG_USERNAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_TELEPHONE, telePhone);
                        map.put(TAG_USERNAME, userName);

                        // adding HashList to ArrayList
                        productsList.add(map);
                    }
                }else {
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            MainContactos.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            Handler h = new Handler();
            h.postDelayed(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainContactos.this, productsList,
                            R.layout.lista_contactos, new String[] { TAG_TELEPHONE,
                                    TAG_USERNAME},
                            new int[] { R.id.telePhoneContacto, R.id.usernameContacto });
                    // updating listview
                    setListAdapter(adapter);
                }
            }, 500);

        }

    }

In my understanding I use while (cursor.moveToNext()) { phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); To read all the phones of my directory and use this PHP to retrieve the users that only have in the directory.

<?PHP
//Codigo para mostrar contactos

//array para JSON response
$response = array();

if (isset($_POST['phoneNumber'])) {

$phoneNumber = $_POST['phoneNumber'];   

require_once(dirname(__FILE__) . '/db_connect.php');

$db = new DB_CONNECT();

$result = mysql_query("SELECT * FROM usuarios WHERE $phoneNumber = telephone") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["usuarios"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $user = array();
        $user["telephone"] = $row["telephone"];
        $user["username"] = $row["username"];

        // push single product into final response array
        array_push($response["usuarios"], $user);
    }
// success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "Usuarios no encontrados";

        // echo no users JSON
        echo json_encode($response);
    }
}else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

And all that only retrieve me an endless loop

Please I need help, I dont know what to do, if someone can tell where to start.

  • SELECT * FROM usuarios WHERE $phoneNumber = telephone, instead of telephone its $phoneNumber correct? – fortune Dec 20 '14 at 08:15
  • Well, $phoneNumber is the number collected of the directory and I try to compare to the field telephone of my table, and if $phoneNumber is equal to some row in the table, that row is charged in the listview. I dont know if I'm wrong. – Erick Justiniano Dec 20 '14 at 18:54

0 Answers0