从Android中的MySQL数据库中获取数据到各自的edittexts

时间:2016-04-10 14:51:42

标签: php android mysql json database

我的应用程序有一个An Activity,它从MySQL中检索数据,以便在单击按钮时编辑文本

1)正在附加活动代码,php代码和布局代码。

编译时没有错误,log-cat中有一些错误。对话框打开,继续运行几秒钟然后解除,但编辑文本字段没有被MySQL的数据填满。

2)MySQL中表的名称是services_provider_details 1,其中包含各自的列(Service_Provider_ID,Name,Address,Pin-code,Phone_Number,DOB,Adhara_Number,Price,Specialization,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代码
  2. 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文件?

1 个答案:

答案 0 :(得分:0)

HttpPost不会返回任何数据。从Android更改您的PHP代码和http请求以使用GET而不是POST

你不是&#34;张贴&#34;无论如何。 Http GET是正确的选择。