簡體   English   中英

從android中的mysql數據庫檢索edittext中的數據

[英]Retrieve data in edittext from mysql database in android

我的應用程序具有一個“活動”,該活動可從MySql中檢索數據以在單擊按鈕時編輯文本

  1. 附有活動代碼,php代碼和布局代碼。

編譯時沒有錯誤,logcat中也有錯誤。對話框打開,繼續運行幾秒鍾,然后關閉,但是編輯文本字段沒有被MySQL數據填充。

  1. MySQL中表的名稱為services_provider_details1,其中包含相應的列(Service_Provider_ID,名稱,地址,密碼,電話號碼,DOB,Aadhar_Number,Price,Specialisation,Status)。

這是我的代碼

public class Update extends  Activity {


EditText  et_spid,et_fullname,et_address,et_sid,et_aadhar,et_dob,et_phone,et_pincode,et_price,et_status,et_spl ;
Spinner spinner;
Button update_button, go;
String spid;

InputStream is=null;
String result=null;
String line=null ;
String myJSON;
JSONArray peoples = null;



    public static final String TAG_SUCCESS="success";
    public static  final String TAG_PROVIDERS = "providers";
    public static  final String TAG_SPID = "spid";
    public static  final String TAG_NAME = "Name";
    public static  final String TAG_ADDRESS = "Address";
    public static  final String TAG_PINCODE = "Pincode";
    public static final  String TAG_PHONENUMBER = "Phone_Number";
    public static  final String TAG_AADHARNUMBER = "Aadhar_Number";
    public static  final String TAG_PRICE = "price";
    public static  final String TAG_SPL = "spl";
    public static  final String TAG_STATUS = "status";



    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update);
          et_spid = (EditText) findViewById(R.id.edittext_spid_insert);
          et_fullname = (EditText) findViewById(R.id.edittext_name_insert);
          et_address = (EditText) findViewById(R.id.edittext_address_insert);
          et_pincode = (EditText) findViewById(R.id.edittext_pin_insert);
          et_phone = (EditText) findViewById(R.id.edittext_phone_insert);
          et_aadhar = (EditText) findViewById(R.id.edittext_aadhar_insert);
          spinner = (Spinner) findViewById(R.id.spinner1);

        et_price =(EditText) findViewById(R.id.edittext_sid_price);
        et_status = (EditText) findViewById(R.id.edittext_sid_status);
        et_spl =(EditText) findViewById(R.id.edittext_sid_speacialisation); 
         update_button = (Button) findViewById(R.id.button_update);
         go = (Button)findViewById(R.id.button_go);



        go.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                spid=et_spid.getText().toString();
                new go().execute();
            }
        });
        }

    class go extends AsyncTask<String, String, String>
    {
        ProgressDialog progress;
        String Url="http://192.168.2.7/myapp/update.php";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        String res = "";
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            progress = ProgressDialog.show(Update.this, "Loading Details",
                    "Please wait..", false);
        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub





        try {

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Url);
            nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("spid",spid));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.e("pass 1", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
    }    
         try{

            BufferedReader reader = new BufferedReader
                (new InputStreamReader(is,"utf-8"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
        {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            Log.e("pass 2", "connection success ");
    }
         catch(Exception e)
        {
        Log.e("Fail 2", e.toString());
    }     


        return res;

     }
        protected void onPostExecute(String response) {
            myJSON = response ;
            try
            {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray("result");

                for (int i = 0; i < peoples.length(); i++) {
                    JSONObject c = peoples.getJSONObject(i);


             String Name=(c.getString("Name"));
                String Address=(c.getString("Address"));
                String Pincode=(c.getString("Pincode"));
                String Phone_Number=(c.getString("Phone_Number"));
                String Aadhar_Number=(c.getString("Aadhar_Number"));
                String price=(c.getString("price"));
                String spl=(c.getString("spl"));
                String status=(c.getString("status"));


                et_fullname.setText(Name);
                 et_address.setText(Address);
                 et_pincode.setText(Pincode);
                 et_phone.setText(Phone_Number);
                 et_aadhar.setText(Aadhar_Number); 
                 et_price.setText(price);
                et_spl.setText(spl);
                et_status.setText(status);




            } 
            }
             catch(Exception e)
            {

                 e.printStackTrace();
            }
progress.dismiss();
        }
    }


}
  1. PHP代碼
mysql_connect("localhost","root","");  
mysql_select_db("myapp");  



     $spid = $_POST['spid'];


    $sql="select * from services_provider_details1 WHERE Service_Provider_ID='$spid' " ;
    $r=mysql_query($sql);
    $result = array();
    while($row=mysql_fetch_array($r))
    {
        array_push($result,
array('Name'=>$row[1],'Address'=>$row[2],'Pincode'=>$row[3],'Phone_Number'=>$row[4],'Aadhar_Number'=>$row[6],'price'=>$row[8],'spl'=>$row[9],'status'=>$row[10]));
    }

    echo json_encode(array("result"=>$result));

mysql_close();

?>

程序運行時的Logcat

04-10 13:24:57.105: W/Trace(1859): Unexpected value from nativeGetEnabledTags: 0
04-10 13:24:57.145: W/Trace(1859): Unexpected value from nativeGetEnabledTags: 0
04-10 13:24:57.145: W/Trace(1859): Unexpected value from nativeGetEnabledTags: 0
04-10 13:24:57.225: W/System.err(1859): org.json.JSONException: End of input at character 0 of 
04-10 13:24:57.296: W/System.err(1859):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
04-10 13:24:57.296: W/System.err(1859):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
04-10 13:24:57.306: W/System.err(1859):     at org.json.JSONObject.<init>(JSONObject.java:154)
04-10 13:24:57.325: W/System.err(1859):     at org.json.JSONObject.<init>(JSONObject.java:171)
04-10 13:24:57.325: W/System.err(1859):     at com.example.homerun.Update$go.onPostExecute(Update.java:197)
04-10 13:24:57.345: W/System.err(1859):     at com.example.homerun.Update$go.onPostExecute(Update.java:1)
04-10 13:24:57.357: W/System.err(1859):     at android.os.AsyncTask.finish(AsyncTask.java:631)
04-10 13:24:57.376: W/System.err(1859):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-10 13:24:57.376: W/System.err(1859):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-10 13:24:57.396: W/System.err(1859):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 13:24:57.396: W/System.err(1859):     at android.os.Looper.loop(Looper.java:137)
04-10 13:24:57.406: W/System.err(1859):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-10 13:24:57.426: W/System.err(1859):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 13:24:57.436: W/System.err(1859):     at java.lang.reflect.Method.invoke(Method.java:511)
04-10 13:24:57.436: W/System.err(1859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 13:24:57.465: W/System.err(1859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 13:24:57.465: W/System.err(1859):     at dalvik.system.NativeStart.main(Native Method)
04-10 13:24:57.475: I/Choreographer(1859): Skipped 113 frames!  The application may be doing too much work on its main thread.
04-10 13:24:58.285: W/Trace(1859): Unexpected value from nativeGetEnabledTags: 0
04-10 13:24:58.285: W/Trace(1859): Unexpected value from nativeGetEnabledTags: 0

並且還請告訴我,是否需要XML文件?

主要問題似乎是服務器端的php代碼,更具體地說是本節:

$spid=$_REQUEST['spid'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$pin=$_REQUEST['pin'];
$phone=$_REQUEST['phone'];
$aadhar=$_REQUEST['aadhaar'];
$price=$_REQUEST['price'];
$spl=$_REQUEST['spl'];
$status=$_REQUEST['status'];

您無需檢查鍵$_REQUEST是否存在於數組中,並且當鍵不存在時,它將觸發通知。 這是預期的行為。

要解決此問題,您應該更新每行,以便它將檢查密鑰是否存在,並且僅在存在密鑰時對其進行訪問:

$spid = isset($_REQUEST['spid']) ? $_REQUEST['spid'] : null;

在PHP 7(由於使用mysql_函數,因此未使用)中,您可以使用較短的語法執行相同的操作:

$spid = $_REQUEST['spid'] ?? null;

由於您在查詢的WHERE子句中使用$spid ,因此您可能要確保該字段存在。 這也需要進行明確的檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM