gpt4 book ai didi

android - 如何将联系人姓名和电话号码发送到MySQL数据库?

转载 作者:行者123 更新时间:2023-11-29 19:24:52 26 4
gpt4 key购买 nike

我想通过 PHP 将联系人姓名和号码存储到 MySQL。问题是插入了空值的数据。代码正确吗?主Activity.java

public class Activity extends NavigationActivity {

TextInputEditText name1,mobile1;

private static final String TAG = Activity.class.getSimpleName();
private static final int REQUEST_CODE_PICK_CONTACTS = 1;
private Uri uriContact;
private String contactID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offers);

name1 = (TextInputEditText) findViewById(R.id.reference_new_connection_name_1);
mobile1 = (TextInputEditText) findViewById(R.id.reference_new_connection_mobile_1);

name1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
name1.setText("");
mobile1.setText("");
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
}
}
});

//This is a Button
public void sendReference(View view) {

sendContacts(retrieveContactName(),retrieveContactNumber());
sendReference();

}

public void sendContacts(final String name, final String mobile)
{
final ProgressDialog progressDialog = new MyCustomProgressDialog(Activity.this);
progressDialog.setCancelable(false);
progressDialog.show();

if (ConnectivityReceiver.isConnected()) {
deleteCache(Activity.this);

StringRequest login_request = new StringRequest(Request.Method.POST, Config.SEND_REFERENCE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response", response);
progressDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
if (error instanceof NoConnectionError) {
noConnection();
} else {
Log.e("Error",error.toString());
}
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put("user_id",id);
params.put("Name",name);
params.put("Mobile",mobile);
return params;
}

};

RequestQueue login = Volley.newRequestQueue(Activity.this);
login.add(login_request);
}
else
{
noConnection();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();

retrieveContactName();
retrieveContactNumber();

name1.setText(retrieveContactName());
mobile1.setText(retrieveContactNumber());

name1.setEnabled(false);
mobile1.setEnabled(false);
}
}

private String retrieveContactNumber() {

String contactNumber = null;

// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);

if (cursorID.moveToFirst()) {

contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}

cursorID.close();
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

new String[]{contactID},
null);

if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
return contactNumber;
}

private String retrieveContactName() {

String contactName = null;

Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);

if (cursor.moveToFirst()) {

contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}

cursor.close();
return contactName;
}


public void sendReference()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Reference Sent..")
.setMessage("Thank You..")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Activity.this,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}

}

最佳答案

这个方法有很大的问题......这里为每个联系人上传到服务器生成请求。

这可能会给服务器带来不必要的流量

我们可以使用 json 数组:

代码:

private fun getContacts(): JsonArray {

val resolver: ContentResolver = contentResolver
val cursor = resolver.query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null
)
val mainJsonArray: JsonArray = JsonArray()
if (cursor!!.count > 0) {
while (cursor.moveToNext()) {
val personJsonObj: JsonObject = JsonObject()
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
//val phoneNumber = (cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER) )).toInt()
personJsonObj.addProperty("NAME", name.replace("\\", "\\\\").replace("'", "\\'").replace("\"", "\\\""))

val orgCursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI, null,
ContactsContract.Data.CONTACT_ID + "=?", arrayOf(id), null
)

val phoneJsonArray = JsonArray()
val emailJsonArray = JsonArray()

if (orgCursor!!.count > 0) {
while (orgCursor.moveToNext()) {
if (orgCursor!!.getString(orgCursor.getColumnIndex(ContactsContract.Data.MIMETYPE)).equals(
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
)
) {
val companyName = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.Data.DATA1))
val designation = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.Data.DATA4))
personJsonObj.addProperty(
"ORGANIZATION",
companyName.replace("\\", "\\\\").replace("'", "\\'").replace("\"", "\\\"")
)
personJsonObj.addProperty(
"DESIGNATION",
designation.replace("\\", "\\\\").replace("'", "\\'").replace("\"", "\\\"")
)

} else if (orgCursor!!.getString(orgCursor.getColumnIndex(ContactsContract.Data.MIMETYPE)).equals(
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
)
) {
val phoneNum = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.Data.DATA1))
phoneJsonArray.add(phoneNum.replace(" ", "").replace("-", ""))
} else if (orgCursor!!.getString(orgCursor.getColumnIndex(ContactsContract.Data.MIMETYPE)).equals(
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
)
) {
val emailAddr = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.Data.DATA1))
emailJsonArray.add(emailAddr.replace(" ", ""))
}
}
}
orgCursor.close()
personJsonObj.add("EMAIL_LIST", emailJsonArray)
personJsonObj.add("PHONE_NUMBERS", phoneJsonArray)
mainJsonArray.add(personJsonObj)
}

}
cursor.close()
return mainJsonArray
}

生成的 json 数组:

[{
"NAME": "Test User Test User Last Name",
"ORGANIZATION": "Some Company",
"DESIGNATION": "Associate",
"EMAIL_LIST": ["testuser@organiztion.com", "testuser1@org.com"],
"PHONE_NUMBERS": ["123456789", "9808776"]
}, {
"NAME": "User One",
"ORGANIZATION": "Test Company",
"DESIGNATION": "Owner",
"EMAIL_LIST": ["abc1@test.com", "abc2@test.com", "abc3@test.com", "abc4@test.com"],
"PHONE_NUMBERS": ["7777", "8888", "9999"]
}]

关于android - 如何将联系人姓名和电话号码发送到MySQL数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com