-4

对于此代码,它总是给我:{“消息”:“缺少必填字段”,“成功”:0}

这意味着它没有进入 if 身体!它总是去别的地方!我不知道为什么!

reg_Doc_to_Patient.php
==========

    <?php

    /*
     * Following code will update a product information
     * A product is identified by product id (pid)
     */

    // array for JSON response
    $response = array();

    // check for required fields
    if(isset($_POST['id']) && isset($_POST['Doc_id']) ) {    


        $id = $_POST['id'];
        $Doc_id = $_POST['Doc_id'];

        // include db connect class
        require_once __DIR__ . '/db_connect.php';

        // connecting to db
        $db = new DB_CONNECT();

        // mysql update row with matched pid
        $result = mysql_query("UPDATE patients SET  Doc_id = '$Doc_id' WHERE id = $id");

        // check if row inserted or not
        if ($result) {
            // successfully updated
            $response["success"] = 1;
            $response["message"] = "Product successfully updated.";

            // echoing JSON response
            echo json_encode($response);
        } else {

        }
    } else {
        // required field is missing
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";

        // echoing JSON response
        echo json_encode($response);
    }
    ?>


AllRequests.java
==========

    package com.example.rpm;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Toast;

    import com.example.rpm.Accept.SaveProductDetails;
    import com.example.rpm.AllDoctor.LoadAllProducts;

    public class AllRequests extends ListActivity {

        // Progress Dialog
            private ProgressDialog pDialog;

            // Creating JSON Parser object
            JSONParser jParser = new JSONParser();

            JSONParser jsonParser = new JSONParser();


            ArrayList<HashMap<String, String>> productsList;

            // url to get all products list
            private static String url_all_products = "http://10.0.2.2/RPM-connect/get_all_requests.php";
            private static String url_edit_products = "http://10.0.2.2/RPM-connect/reg_Doc_to_Patient.php";



            // JSON Node names
            private static final String TAG_SUCCESS = "success";
            private static final String TAG_DOCTOR = "products";

            private static final String TAG_Patient_ID = "Patient_ID";
            private static final String TAG_Doctor_ID= "Doctor_ID";

            private static  String id=""; // of patient
            private static  String Doc_id="";// of Doctor 

            // products JSONArray
            JSONArray doctor = null;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.requests);

                // Hashmap for ListView
                productsList = new ArrayList<HashMap<String, String>>();

                // Loading products in Background Thread
                new LoadAllProducts().execute();

                // Get listview
                ListView lv = getListView();

                // on seleting single product
                // launching Edit Product Screen
                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long idd) {
                        // getting values from selected ListItem


                        final String p_Id = ((TextView) view.findViewById(R.id.p_id)).getText().toString();
                        final String d_Id = ((TextView) view.findViewById(R.id.d_id)).getText().toString();


                        id= p_Id;
                        Doc_id= d_Id;

                        new VerivyDocToPatient().execute();

                        Toast.makeText(getApplicationContext(), "yes", 500).show();
                        // Starting new intent
                        //Intent in = new Intent(getApplicationContext(),   AllRequests.class);
                        // sending pid to next activity
                        //in.putExtra(TAG_ID, Id);


                        // starting new activity and expecting some response back
                    //  startActivityForResult(in, 100);
                    }
                });

            }

            // Response from Edit Product Activity
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                // if result code 100
                if (resultCode == 100) {
                    // if result code 100 is received 
                    // means user edited/deleted product
                    // reload this screen again
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }

            }

            /**
             * Background Async Task to Load all product by making HTTP Request
             * */
            class LoadAllProducts extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(AllRequests.this);
                    pDialog.setMessage("Loading doctors. Please wait...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(false);
                    pDialog.show();
                }

                /**
                 * getting All products from url
                 * */
                protected String doInBackground(String... args) {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    // getting JSON string from URL
                    JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

                    // Check your log cat for JSON reponse
                    Log.d("All Products: ", json.toString());

                    try {
                        // Checking for SUCCESS TAG
                        int success = json.getInt(TAG_SUCCESS);

                        if (success == 1) {
                            // products found
                            // Getting Array of Products
                            doctor = json.getJSONArray(TAG_DOCTOR);

                            // looping through All Products
                            for (int i = 0; i < doctor.length(); i++) {
                                JSONObject c = doctor.getJSONObject(i);


                                // Storing each json item in variable

                                String Patient_ID = c.getString(TAG_Patient_ID);
                                String Doctor_ID = c.getString(TAG_Doctor_ID);



                                // creating new HashMap
                                HashMap<String, String> map = new HashMap<String, String>();

                                // adding each child node to HashMap key => value

                                map.put(TAG_Patient_ID, Patient_ID);
                                map.put(TAG_Doctor_ID, Doctor_ID);



                                // adding HashList to ArrayList
                                productsList.add(map);
                            }
                        } else {
                            // no products found
                            // Launch Add New product Activity
                            Intent i = new Intent(getApplicationContext(),
                                    Adddoctor.class);
                            // Closing all previous activities
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }

                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog after getting all products
                    pDialog.dismiss();
                    // updating UI from Background Thread
                    runOnUiThread(new Runnable() {
                        public void run() {
                            /**
                             * Updating parsed JSON data into ListView
                             * */
                            ListAdapter adapter = new SimpleAdapter(
                                    AllRequests.this, productsList,
                                    R.layout.list_requests, new String[] { TAG_Doctor_ID,TAG_Patient_ID },  
                                    new int[] { R.id.p_id, R.id.d_id
                                            });
                            // updating listview
                            setListAdapter(adapter);


                        }
                    });

                }

            }

            /**
             * Background Async Task to  Save product Details
             * */
            class VerivyDocToPatient extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(AllRequests.this);
                    pDialog.setMessage("verifying...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                /**
                 * Saving product
                 * */
                protected String doInBackground(String... args) {

                    // getting updated data from EditTexts
                    //String ID = txtID.getText().toString();
                    /*String Fname = txtFname.getText().toString();
                    String Sname = txtSname.getText().toString();
                    String Lname = txtLname.getText().toString();
                    String Specialist = txtSpecialist.getText().toString();
                    String Gender = txtGender.getText().toString();
                    String Workphone = txtWorkphone.getText().toString();
                    String password = txtpassword.getText().toString();
                    */

                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();

                    params.add(new BasicNameValuePair(TAG_Patient_ID, id));
                    params.add(new BasicNameValuePair(TAG_Doctor_ID, Doc_id));

                    // sending modified data through http request
                    // Notice that update product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_edit_products,
                            "POST", params);
                    Log.d("aaaa",json.toString());

                    // check json success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);

                        if (success == 1) {
                            // successfully updated
                            Intent i = getIntent();
                            // send result code 100 to notify about product update
                            setResult(100, i);
                            finish();
                        } else {
                            // failed to update product
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }


                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once product uupdated
                    pDialog.dismiss();
                }
            }

    }

有什么帮助吗?

4

2 回答 2

3

在您的 Java 代码中,您使用Patient_IDandDoctor_ID作为参数名称,但 PHP 正在检查idand Doc_id

于 2013-03-21T23:32:29.937 回答
0

我不确定这是否是实际问题。

isset($_POST['id']) && isset($_POST['Doc_id'])

params.add(new BasicNameValuePair(TAG_Patient_ID, id))

params.add(new BasicNameValuePair(TAG_Doctor_ID, Doc_id))

看看您是否在此处检查正确的名称。

于 2013-03-21T23:41:00.723 回答