我正在检索 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;
}