我正在创建一个 android 应用程序,我想在其中从 mysql 访问数据库以获取用户名、电话号码和电子邮件地址。我想比较android手机联系人列表中的电话号码和mysql数据库中的电话号码。为此,我使用 2 arraylist 第一个用于电话联系,第二个用于 mysql 电话号码。我的主要问题是当我比较两个数组列表时,我没有显示任何结果。我在这里附上代码,请有人帮我解决这个问题。
public class PhoneNoActivity extends Activity{
JSONArray jArray,jArray1;
JSONObject jobj;
String result = null,phone=null;
InputStream is = null;
StringBuilder sb=null;
double lat=0;
double lon=0;
String user=null;
ArrayList<NameValuePair> nameValuePairs;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
String phno=null;
ArrayList<String> cntPhone=new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(phoneNo.length()>10) {
phno=phoneNo.subSequence(phoneNo.length()-10, phoneNo.length()).toString();
// Log.e(name, phno);
}
cntPhone.add(phoneNo);
}
pCur.close();
}
}
}
ArrayList<String> cntOnline=new ArrayList<String>();
try {
nameValuePairs = new ArrayList<NameValuePair>();
/*String phone=null;
for(int k=0;k<=cntPhone.size();k++) {
Log.e("k",k+"");
phone=cntPhone.get(k);
nameValuePairs.add(new BasicNameValuePair("phone", phone));
*/
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/ah_login_api/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("result=", result);
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
user=json_data.getString("user");
phone=json_data.getString("phone");
lat=json_data.getDouble("email");
cntOnline.add(phone);
}
for(String s:cntOnline) {
for(String s1:cntPhone ) {
if(s.equals(s1)) {
Log.e("match found", phone);
}
}
}
/*
for(int l=0;l<cntOnline.size();l++) {
Log.e("loop start", ""+l);
for(int k=0;k<cntPhone.size();k++) {
if(cntPhone.get(k).trim().equals(cntOnline.get(l).trim())) {
Log.e("match found", phone);
}
}
}*/
//}
} catch(Exception ex) {
Log.e("Exception in ",ex.toString());
}
}
}
这是我访问详细信息的 php 代码
<?php
mysql_connect("localhost","root","");
mysql_select_db("MyContact");
$sql=mysql_query("select * from newuser");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>