Why is phonenumber
in my HashMap
giving me a null value? The idea is that I loop through all contacts on my phone. In my try - catch
statement, which is working fine, I see all my contacts in logcat with :
System.out.println("JSON: " + phonenumber);
But with my code System.out.println("contact is : " + phonenumber);
later on in the Hashmap I get in logcat :
contact is : null
I want to post all the phone numbers to a MySql database, can you tell me what I am doing wrong ?
public class MainActivity extends AppCompatActivity {
// this is the php file we are contacting with Volley
private static final String CHECKPHONENUMBER_URL = "http://www.sitetocheckwithVolley.com/filetocheckwithVolley.php";
//we are posting phoneNo, which in PHP is phonenumber
public static final String KEY_PHONENUMBER = "phonenumber";
//alContacts is a list of all the phone numbers
public static final ArrayList<String> alContacts = new ArrayList<String>();
Button buttonCheck;
TextView textView;
String phonenumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCheck = (Button) findViewById(R.id.buttonCheck);
textView = (TextView) findViewById(R.id.textView);
//get the names and phone numbers of all contacts in phone book
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
alContacts.add(phoneNo);
// break;
}
pCur.close();
}
}
}
buttonCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CheckifUserisContact();
}
});
}
private void CheckifUserisContact() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObjectContact = new JSONObject();
//put all phone contacts into jsonObjectContact
for (int i = 0; i < alContacts.size(); i++)
{
// jsonObjectContact will be of the form {"phone_number":"123456789"}
jsonObjectContact.put("phone_number", alContacts.get(i));
//make a new string phonenumber for each JSON contact
phonenumber = jsonObjectContact.getString("phone_number");
textView.append(phonenumber + " \n");
System.out.println("JSON: " + phonenumber);
}
} catch (final JSONException e) {
Log.e("FAILED", "Json parsing error: " + e.getMessage());
} }
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//The KEY, KEY_PHONENUMBER = "phonenumber" . In PHP we will have $_POST["phonenumber"]
//The VALUE, phonenumber, will be of the form "12345678"
params.put(KEY_PHONENUMBER,phonenumber);
System.out.println("contact is : " + phonenumber);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}