从android中的mysql数据库中检索edittext中的数据

My app has an An Activity which retrieves the data from MySql in to edit texts on clicking a button

  1. Activity code , php code and layout code is being attached .

There is no error in the compile time and there is some error in the logcat .. The dialog opens up , keep running for seconds and then dismisses but the edit text fields are not filled up by the data from MySQL.

  1. The name of the table in the MySQL is services_provider_details1 which contains respective columns ( Service_Provider_ID,Name,Address, Pincode, Phone_Number,DOB,Aadhar_Number,Price,Specialisation,Status ).

Here is my code

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 + "
");
                }
                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 code
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 of the programme when run

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

And Please also tell me that Is XML File required?

The main problem seems to be the server side php code, more specifically this section:

$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'];

You do not check whether or not the keys exist in the array $_REQUEST and when a key does not exist it will trigger a notice. That is expected behaviour.

To fix that, you should update each line so it will check whether the key exists and only access it if it is present:

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

In PHP 7 (which you are not using, since you are using mysql_ functions), you would be able to do the same in a shorter syntax:

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

Since you use $spid in the WHERE clause of your query, you may want to make sure that field is present. That also requires an explicit check.