gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:09:25 28 4
gpt4 key购买 nike

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

  1. 正在附加 Activity 代码、php 代码和布局代码。

编译时没有错误,logcat 中有一些错误。对话框打开,继续运行几秒钟,然后关闭,但编辑文本字段没有被来自 MySQL 的数据填充。

  • MySQL 中的表名称为 services_provider_details1,其中包含各个列(Service_Provider_ID、名称、地址、Pincode、电话号码、出生日期、Aadhar_Number、价格、特化、状态)。
  • 这是我的代码

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


    }
  • 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中是否存在键,当键不存在时,它将触发通知。这是预期的行为。

    要解决这个问题,您应该更新每一行,以便它检查 key 是否存在,并且仅在存在时才访问它:

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

    在 PHP 7 中(您没有使用它,因为您使用的是 mysql_ 函数),您可以使用更短的语法执行相同的操作:

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

    由于您在查询的 WHERE 子句中使用 $spid,因此您可能需要确保该字段存在。这也需要明确的检查。

    关于php - 从android中的mysql数据库中检索edittext中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36520403/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com