Android 如何将联系人姓名和号码发送到MySQL数据库?
我想通过PHP将联系人姓名和号码存储到MySQL。问题是插入了空值的数据。代码正确吗? Main Activity.javaAndroid 如何将联系人姓名和号码发送到MySQL数据库?,android,mysql,android-intent,android-volley,Android,Mysql,Android Intent,Android Volley,我想通过PHP将联系人姓名和号码存储到MySQL。问题是插入了空值的数据。代码正确吗? Main Activity.java public class Activity extends NavigationActivity { TextInputEditText name1,mobile1; private static final String TAG = Activity.class.getSimpleName(); private static final in
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();
}
}
我非常抱歉,伙计们。。我浪费了你的黄金时光。。最后我得到了答案,。。Php开发人员犯了一个错误。。他更改了我发送的参数值。。哈维·弗莱彻是对的,。。感谢您的响应人员。列类型可能设置为int,这意味着从一开始就删除0。此方法有一个大问题。。。。。。 这里为每个联系人上传到服务器生成请求 这可能会给服务器造成不必要的流量 相反,我们可以使用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"]
}]
代码:
生成的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"]
}]
请提供有关表格的数据。列类型可能被设置为int,这意味着0将从一开始就被删除。您是否在清单中授予权限?如果您在棉花糖上运行,您还应该授予运行时权限。我将我的表值设置为varchar。。没问题我加了棉花糖的许可。。我和Kitkat核实过了。嗨,我已经添加了我的答案,请点击它说它回答了你的问题。谢谢