2017-02-14 52 views
0

我想存儲聯繫人姓名&通過PHP的MySQL數量。問題是數據插入空值。代碼是否正確? 主要Activity.java如何將聯繫人姓名和號碼發送到MySQL數據庫?

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(); 
    } 

} 
+0

請提供您的餐桌數據。列類型可以設置爲int,這意味着0將從頭開始刪除。 –

+0

你有清單權限嗎? –

+0

如果你跑過棉花糖,你還應該給予運行時許可。 –

回答

0

列類型可能被設置爲int,這意味着0將從開始被刪除。

0

我Extreamly對不起你們。我浪費了你的黃金時期。最後我得到了答案,... PHP開發人員犯了一個錯誤。他改變了我發送的PARAMS值。哈維弗萊徹是正確的,..謝謝你的回覆傢伙..

+0

嗨,我已經添加了我的答案,請點擊它說它回答了你的問題。謝謝 –

相關問題