当我在jsonarray中解析字符串时出现错误

时间:2016-02-01 06:58:13

标签: android

{
    "0": "261",
    "1": "0",
    "2": "0",
    "3": "",
    "4": "0",
    "5": "0",
    "6": "",
    "7": "<br>",
    "8": "",
    "9": "",
    "10": "",
    "11": "",
    "12": "",
    "13": "",
    "14": "http://www.yandy.com/Seduce-GoGo-Boots.php",
    "15": "2016-01-15 14:44:38",
    "16": "",
    "17": "",
    "18": "2",
    "d_id": "261",
    "m_id": "0",
    "c_id": "0",
    "title": "",
    "price": "0",
    "list_price": "0",
    "descri": "",
    "how_to_get": "<br>",
    "seotitle": "",
    "seokey": "",
    "seometakey": "",
    "seoh1": "",
    "link": "",
    "review": "",
    "image": "http://www.yandy.com/Seduce-GoGo-Boots.php",
    "datetime": "2016-01-15 14:44:38",
    "fp": "",
    "my_title": "",
    "country": "2"
}

这是我的代码

class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(StoreData.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

                Log.d(" i m in try block:",json.toString());


                if (id == 0) {
                    // products found
                    // Getting Array of Products
                     Log.d("in if: ", json.toString());
                      if (json.has("title")) {
                            String projectnumber1 = json.getString("title");
                            Log.d("in if: ", projectnumber1 );
                        }
                      if (json.has("image")) {
                            String projectnumber1 = json.getString("image");
                            Log.d("in if: ", projectnumber1 );
                        }
                      String notes = json.getString("image");
                      store = new JSONArray(notes);
                    //store = json.getJSONArray(TAG_IMAGE);


                    // Check your log cat for JSON response
                    Log.d("Array data: ", store.toString());

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

                        // Storing each json item in variable
                        String image=c.getString(TAG_IMAGE);
                       // String name = c.getString(TAG_TITLE);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_IMAGE, image);
                      //  map.put(TAG_TITLE, name);

                        // adding HashList to ArrayList
                        storeList.add(map);
                        Log.d("Array data list: ", storeList.toString());

               }// else {
                    // no products found
                    // Launch Add New product Activity
                   // Intent i = new Intent(getApplicationContext(),
                            //Third.class);
                    // Closing all previous activities
                  //  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   // startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

1 个答案:

答案 0 :(得分:1)

您在String变量中获取图片并尝试将其转换为此行的JSONArray

String notes = json.getString("image");
store = new JSONArray(notes);

此处的注释将与此"http://www.yandy.com/Seduce-GoGo-Boots.php"类似,并且您将其转换为JSONArray。所以错误。