0
我一直在嘗試一段時間來將我手機中的所有電話號碼轉換爲JSON,並將它們上傳到一個MySQL數據庫中,你能幫忙嗎?我現在專注於Android的一面 - php將在以後。如何將我的Arraylist轉換爲JSON並將其發佈到MySQL?
所以,我把我所有的數字的ArrayList - 當你點擊buttonCheck
按鈕,你可以看到他們都在logcat中,但畢竟我讀過方法size
和length
仍然不在我alContacts
陣列的認可。我相信這是我應該轉換爲JSON(?)的軌道。這裏是我的代碼:
public class MainActivity extends AppCompatActivity {
// this is the php file name where to select from the database, the user's phone number
private static final String CHECKPHONENUMBER_URL = "http://www.example.com/myfile.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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCheck = (Button) findViewById(R.id.buttonCheck);
//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) {
System.out.println("Print the contacts array : ");
System.out.println(alContacts);
CheckifUserisContact();
}
});
}
// send the numbers in one POST to the mySql db with Volley
private void CheckifUserisContact() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("failure")) {
Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "succeeded", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
//********* HAVING TROUBLE HERE *******************
// JSONObject jsonObject=new JSONObject();
JSONArray jsonArray = new JSONArray();
// jsonObject.put("alContacts",jsonArray);
//for(int i=0;i<=alContacts.size;i++){}
//*****************************************************
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_PHONENUMBER, jsonArray.toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
其實我認爲你的代碼塊應該在我的'public void onR esponse'部分,而不是'Response.ErrorListener'。非常好,謝謝! – CHarris
我的榮幸:)....最佳做法是在創建字符串/ JSONObject請求之前構建JSON。在您的onResponse中,如果成功將您的conatct JSON數據發送到服務器,您將從服務器獲得響應JSON, – FAT