I have some contact numbers saved in a remote server and I have more than 100 numbers saved on my Android phone.
What I want to do is compare the numbers present on the remote server and the phone numbers in the Android phone and display only the matching numbers in a Listview i.e if 1234 is saved on my remote server and 1234, 5678,1000,etc are saved on my Android phone, then only the number 1234 should be displayed on the Listview.
I have done the following coding and it works fine and gives me the result i want, but the problem is that the time consumption is very high.
Can anyone suggest me whether it is possible to get the matching contacts without using a loop or is there any way to reduce the time consumption? My codes are as below, please guide me step by step.
AsyncTask<Void, Void,Void> t = new AsyncTask<Void, Void,Void>()
{
@Override
protected Void doInBackground(Void... arg0) {
String phoneNumber = null;
//String email = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
StringBuffer output = new StringBuffer();
if(getActivity()!=null)
{
ContentResolver contentResolver =getActivity().getApplicationContext().getContentResolver();
//contentResolver=getActivity().getContentResolver().getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
output.append("\n First Name:" + name);
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
String p=phoneNumber;
output.append("\n Phone number:" + phoneNumber);
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:80/contactcheck.php");
HttpResponse response1 = httpclient.execute(httppost);
HttpEntity entity = response1.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try
{
JSONArray jArray = new JSONArray(result);
String s11,s12,s13;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
s11=json_data.getString("PhoneId");
s12=json_data.getString("path");
s13=json_data.getString("UserId");
Log.w("matched",""+s11+p);
Log.i("path from json",""+s12);
if(p.compareTo(s11)==0){
int count=0;
count=db.getcountoffriends(s11);
if(count==0)
{
queryValues.put("c",s11);
queryValues.put("name",name);
queryValues.put("path", s12);
queryValues.put("fbid",s13);
db.insertcontact(queryValues);
imageload(s12,s13,s11);
}
Log.i("imageupload strings",""+s12+""+s13+""+s11);
}
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
}
}
return null;
}
};
t.execute();