我正在获取联系人并将它们放入JSONArray中以将所有联系人上传到服务器。当我提取它们时,我正在创建Log.i();
并正确显示联系人。但是当我在JSON数组上放置相同的值时,它表示它是空的。
以下是代码:
public void phoneandname(Context c) {
if (cc.isOnline(c) == true) {
db = new Database(c);
Cursor phones = c.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
JSONArray jarray = new JSONArray();
db.open();
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
JSONObject json = new JSONObject();
try {
json.put("name", name);
json.put("phone", phoneNumber);
json.put("type", "insert");
jarray.put(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
DBupload upload = new DBupload();
DatosRegistro dr = new DatosRegistro(c);
nvp.add(new BasicNameValuePair("content", String.valueOf(jarray)));
nvp.add(new BasicNameValuePair("token", dr.hash_final(
dr.token_secret(dr.mail(), "mail"), jarray.toString())));
nvp.add(new BasicNameValuePair("id", String.valueOf(dr
.return_id_phone())));
JSONObject json2 = upload.UploaData(nvp,
"http://anonyme.mariomontes.es/contacts/insert");
try {
if (json2.getInt("error") == 1) {
} else {
db.contact_table(name, phoneNumber);
}
} catch (JSONException e) {
e.printStackTrace();
}
db.close();
Log.i("********", jarray.toString());
phones.close();
}
}
如你所见。我正在显示该值,然后我上传到服务器。但是它说内容(数组)是空的,我不知道。