Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法从Mysql列出我的Aandroid应用程序中的数据_Php_Android_Mysql_Json - Fatal编程技术网

Php 无法从Mysql列出我的Aandroid应用程序中的数据

Php 无法从Mysql列出我的Aandroid应用程序中的数据,php,android,mysql,json,Php,Android,Mysql,Json,我是Android开发的新手。我试图在android应用程序中执行数据库CRUD操作。 我可以成功地将数据插入mysql。我使用PHP和JSON对其进行解析。当数据插入数据库时,下一个活动应该是列出数据。问题是没有数据进入我的列出活动。只有活动没有数据。请帮帮我 在这段代码中,我试图显示三个数据,“name,price,description 从数据库mysql中检索数据,并将字符串转换为JSon对象。然后我的Java代码将调用JSon类并列出数据。我可以成功地将数据插入mysql。我正在使用P

我是Android开发的新手。我试图在android应用程序中执行数据库CRUD操作。 我可以成功地将数据插入mysql。我使用PHP和JSON对其进行解析。当数据插入数据库时,下一个活动应该是列出数据。问题是没有数据进入我的列出活动。只有活动没有数据。请帮帮我

在这段代码中,我试图显示三个数据,“name,price,description 从数据库mysql中检索数据,并将字符串转换为JSon对象。然后我的Java代码将调用JSon类并列出数据。我可以成功地将数据插入mysql。我正在使用PHP和JSon解析。当数据插入数据库时,下一个活动应该是列出数据。问题是没有数据出现进入我的列表活动。只有活动没有数据。 另外,我列出的产品的java代码是

公共类AllProductsActivity扩展了ListActivity{

                                // Progress Dialog
                                private ProgressDialog pDialog;

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

                                ArrayList<HashMap<String, String>> productsList;

                                // url to get all products list
                                private static String url_all_products = "http://10.0.2.2/get_all_products.php";

                                // JSON Node names
                                private static final String TAG_SUCCESS = "success";
                                private static final String TAG_PRODUCTS = "products";
                                private static final String TAG_PID = "pid";
                                private static final String TAG_NAME = "name";

                                // products JSONArray
                                JSONArray products = null;

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

                                    // 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 id) {
                                            // getting values from selected ListItem
                                            String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                                                    .toString();

                                            // Starting new intent
                                            Intent in = new Intent(getApplicationContext(),
                                                    EditProductActivity.class);
                                            // sending pid to next activity
                                            in.putExtra(TAG_PID, pid);

                                            // 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(AllProductsActivity.this);
                                        pDialog.setMessage("Loading products. 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
                                                products = json.getJSONArray(TAG_PRODUCTS);

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

                                                    // Storing each json item in variable
                                                    String id = c.getString(TAG_PID);
                                                    String name = c.getString(TAG_NAME);

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

                                                    // adding each child node to HashMap key => value
                                                    map.put(TAG_PID, id);
                                                    map.put(TAG_NAME, name);

                                                    // adding HashList to ArrayList
                                                    productsList.add(map);
                                                }
                                            } else {
                                                // no products found
                                                // Launch Add New product Activity
                                                Intent i = new Intent(getApplicationContext(),
                                                        NewProductActivity.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(
                                                        AllProductsActivity.this, productsList,
                                                        R.layout.list_item, new String[] { TAG_PID,
                                                                TAG_NAME},
                                                        new int[] { R.id.pid, R.id.name });
                                                // updating listview
                                                setListAdapter(adapter);
                                            }
                                        });

                                    }

                                }

                                }


                                Here in this code , am trying to display three data , "name, price, description. The PHP file will 
                                retrieve the data from the database mysql  , and convert the strings into JSon objects. Then my javacode will call the json class and list the data. I can successfully insert the data to the mysql.Am using PHP and JSON parsing for it.When the data inserted into database, the next activity should be listing the data.The problem is no data is coming into my listing activity. only the Activity is coming without data. kindly help me .


                 My JSON class for parsing or converting is as follows

                            This is my JSON parsing code .In this code first i created a function for get json from the url . then the code for making http request for GET and POST method by using if , else statement . In else method i put the code for get method . i converted it into json object , and the above android code will call it and display it in the activity..
                        I used a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url.
                    As we are getting the JSON by making HTTP call, I am adding a Async method to make http calls on background thread. Add the follwing method in your main activity class

                                public JSONParser() {

                                    }

                                    // function get json from url
                                    // by making HTTP POST or GET mehtod
                                    public JSONObject makeHttpRequest(String url, String method,
                                            List<NameValuePair> params) {

                                        // Making HTTP request
                                        try {

                                            // check for request method
                                            if(method == "POST"){
                                                // request method is POST
                                                // defaultHttpClient
                                                DefaultHttpClient httpClient = new DefaultHttpClient();
                                                HttpPost httpPost = new HttpPost(url);
                                                httpPost.setEntity(new UrlEncodedFormEntity(params));

                                                HttpResponse httpResponse = httpClient.execute(httpPost);
                                                HttpEntity httpEntity = httpResponse.getEntity();
                                                is = httpEntity.getContent();

                                            }else if(method == "GET"){
                                                // request method is GET
                                                DefaultHttpClient httpClient = new DefaultHttpClient();
                                                String paramString = URLEncodedUtils.format(params, "utf-8");
                                                url += "?" + paramString;
                                                HttpGet httpGet = new HttpGet(url);

                                                HttpResponse httpResponse = httpClient.execute(httpGet);
                                                HttpEntity httpEntity = httpResponse.getEntity();
                                                is = httpEntity.getContent();
                                            }          

                                        } catch (UnsupportedEncodingException e) {
                                            e.printStackTrace();
                                        } catch (ClientProtocolException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }

                                        try {
                                            BufferedReader reader = new BufferedReader(new InputStreamReader(
                                                    is, "iso-8859-1"), 8);
                                            StringBuilder sb = new StringBuilder();
                                            String line = null;
                                            while ((line = reader.readLine()) != null) {
                                                sb.append(line + "\n");
                                            }
                                            is.close();
                                            json = sb.toString();
                                        } catch (Exception e) {
                                            Log.e("Buffer Error", "Error converting result " + e.toString());
                                        }

                                        // try parse the string to a JSON object
                                         try {
                    if(isValidJson(json))
                    jObj = new JSONObject(json);
              } catch (JSONException e) {
                  Log.v("json String","Result => "+json);
               }

                                        // return JSON String
                                        return jObj;

                                    }
                                }    




This is my JSON parsing code .In this code first i created a function for get json from the url . then the code for making http request for GET and POST method by using if , else statement . In else method i put the code for get method . i converted it into json object , and the above android code will call it and display it in the activity..
                    I used a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url.
                As we are getting the JSON by making HTTP call, I am adding a Async method to make http calls on background thread. 
            I used a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url.
                As we are getting the JSON by making HTTP call, I am adding a Async method to make http calls on background thread.      


    Since nothing is displayed in my activity. i have the following error in my logcat.
            my logcat error is displayed below  .

检查JSonObject是否具有产品的键值

 03-10 03:49:54.491: W/System.err(1559): org.json.JSONException: No value for products

此错误显示的JSONobject没有products键值。

检查日志猫的可能重复您可能会在第`Log.d行(“所有产品:”,json.toString());`@Blacktiger,,我不明白您的意思。您能再详细解释一下吗?我在日志猫中找不到打印
json.toString()的json数据
进入textview@Amadas我们如何检查JSONObject是否具有产品键值?您使用了此代码“Log.d”(“所有产品:”,json.toString())'。您可以显示日志。@Amadas,我在我的日志猫中找不到它。该消息不在。是的,该消息在我的日志猫中。我们如何向我的日志猫显示它activity@NeenuJose第141行的AllProductActivity.java是什么代码?我认为错误是'int success=json.getInt(TAG_success);'或'products=json.getJSONArray(TAG_products);'
 03-10 03:49:54.491: W/System.err(1559): org.json.JSONException: No value for products