2

I am retrieving Android phone's contacts and creating JSONArray of it,the count of cursor which gives me all contacts and length of JSONArray in which i am putting all contacts is same ,this means all the contacts have been successfully put inside JSONArray. but whenever i tried to print JsonArray.toString() in logcat,i get incomplete Json,i.e. it breaks in between?what is the solution to avoid this? Thanks in advance

The code which is retrieving contacts and putting inside 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;
    }
Rohan
  • 188
  • 4
  • 13

1 Answers1

2

Refering to Thread "displaying-more-string-on-logcat" , i think your json string length is too long (>4000 chars).

Community
  • 1
  • 1
MemLeak
  • 4,456
  • 4
  • 45
  • 84