我正在检索Android手机的联系人并创建它的JSONArray,光标计数给了我所有联系人和JSONArray的长度,其中我将所有联系人都放在一起,这意味着所有联系人都已成功放入JSONArray。但每当我试图在logcat中打印JsonArray.toString()时,我都会得到不完整的Json,即。它介于两者之间?避免这种情况的解决方案是什么? 提前致谢
正在检索联系人并放入json
的代码Uri URI = ContactsContract.Contacts.CONTENT_URI;
String[] projection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER };
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Cursor contactCursor = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
Log.d("Alpha","Cursor count is "+contactCursor.getCount());
jsonArray=new JSONArray();
int count=0;
while(contactCursor.moveToNext())
{
String id = contactCursor.getString(contactCursor.getColumnIndex(BaseColumns._ID));
String name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phone = "";
if (Integer
.parseInt(contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = c.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { id },
null);
while (pCur.moveToNext())
{
JSONObject json = new JSONObject();
phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
phone = phone.replace("(", "");
phone = phone.replace(")", "");
phone = phone.replace("-", "");
phone = phone.replace(" ", "");
try
{
json.put("Name", name);
json.put("Number", phone);
Log.d("Alpha",name + " "+phone+" "+count);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonArray.put(json);
}
}
count++;
}
Log.d("Alpha","populated jsonArray is "+jsonArray.length());
String dataToBeSend=jsonArray.toString();
Log.d("Alpha","populated jsonArray to string is "+dataToBeSend);
return null;
}