0

我有这样的代码来显示来自mysql的数据,

它读取数据

protected String doInBackground(String... params) {
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                       // Building Parameters
                //  int success;
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("phone", mPhoneNumber));
                        params.add(new BasicNameValuePair("id_store", id_store));

                        // getting store details by making HTTP request
                        // Note that store details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(Constants.url_store_detail, "GET", params);

                        // check your log for json response
                        Log.d("Single Store Details", json.toString());

                        // json success tag

                            // successfully received store details
                            JSONArray storeObj = json.getJSONArray("store"); // JSON Array

                            // get first product object from JSON Array
                            JSONObject store = storeObj.getJSONObject(0);

                            // product with this pid found
                            // Edit Text
                            urledit = (EditText) findViewById(R.id.ETurledit);
                            storenameedit = (EditText) findViewById(R.id.ETusernameedit);
                            storedescedit = (EditText) findViewById(R.id.ETdescedit);
                            edittype=(Spinner)findViewById(R.id.Stypeedit);

                            String url = json.getString(TAG_URL);

                            // display store data in EditText
                            urledit.setText(store.getString(TAG_URL));
                            storenameedit.setText(store.getString(TAG_NAME));
                            storedescedit.setText(store.getString(TAG_DESC));               

                            if(store.getString(TAG_TYPE).equals("1")){
                                String mine = store.getString("Wordpress");
                                ArrayAdapter myadap = (ArrayAdapter)edittype.getAdapter();

                                int spinnerpos = myadap.getPosition(mine);
                                edittype.setSelection(spinnerpos);

                            }else{
                                String mine = store.getString("Prestashop");
                                ArrayAdapter myadap = (ArrayAdapter)edittype.getAdapter();

                                int spinnerpos = myadap.getPosition(mine);

                                edittype.setSelection(spinnerpos);


                            }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

但它显示这样的错误

10-04 13:08:57.377: DEBUG/dalvikvm(306): GC_EXPLICIT freed 42 objects / 2008 bytes in 260ms
10-04 13:08:59.837: INFO/ActivityManager(59): Starting activity: Intent { cmp=shoop3.android.edu/.editstoreActivity (has extras) }
10-04 13:09:01.148: DEBUG/dalvikvm(1272): GC_FOR_MALLOC freed 1456 objects / 269496 bytes in 198ms
10-04 13:09:01.407: DEBUG/Single Store Details(1272): {"store":{"id_user":"56","id_platform":"2","store_url":"prestashop","store_desc":"dasdasd","id_store":"38","store_logo":null,"store_name":"prestashop","store_curr":null}}
10-04 13:09:01.427: WARN/System.err(1272): org.json.JSONException: Value {"id_user":"56","id_platform":"2","store_url":"prestashop","store_desc":"dasdasd","id_store":"38","store_logo":null,"store_name":"prestashop","store_curr":null} at store of type org.json.JSONObject cannot be converted to JSONArray
10-04 13:09:01.457: WARN/System.err(1272):     at org.json.JSON.typeMismatch(JSON.java:96)
10-04 13:09:01.457: WARN/System.err(1272):     at org.json.JSONObject.getJSONArray(JSONObject.java:548)
10-04 13:09:01.507: WARN/System.err(1272):     at shoop3.android.edu.editstoreActivity$GetStoreDetails$1.run(editstoreActivity.java:189)
10-04 13:09:01.507: WARN/System.err(1272):     at android.os.Handler.handleCallback(Handler.java:587)
10-04 13:09:01.517: WARN/System.err(1272):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 13:09:01.517: WARN/System.err(1272):     at android.os.Looper.loop(Looper.java:123)
10-04 13:09:01.517: WARN/System.err(1272):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-04 13:09:01.528: WARN/System.err(1272):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 13:09:01.528: WARN/System.err(1272):     at java.lang.reflect.Method.invoke(Method.java:521)
10-04 13:09:01.528: WARN/System.err(1272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-04 13:09:01.537: WARN/System.err(1272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-04 13:09:01.577: WARN/System.err(1272):     at dalvik.system.NativeStart.main(Native Method)
10-04 13:09:02.948: INFO/ActivityManager(59): Displayed activity shoop3.android.edu/.editstoreActivity: 3002 ms (total 3002 ms)

这个怎么解决??因为它显示 JSONObject 无法转换为 JSONArray 的错误,我希望有人可以帮助我,感谢您的建议

4

2 回答 2

1

通过查看您的日志,使用它

JSONObject storeObj = json.getJSONObject("store");

代替

JSONArray storeObj = json.getJSONArray("store");

因为 store 是一个 JSON 对象

于 2012-10-04T06:34:13.587 回答
0
JSONObject json = jsonParser.makeHttpRequest(Constants.url_store_detail, "GET", params);

如您所见,这里的 json 是一个JSONObject. 和这里

JSONArray storeObj = json.getJSONArray("store");

JSONObject你试图得到一个JSONArray.

因此,与其将 jsonJSONObject声明为,不如将其声明为JSONArray.

然后删除 storeObj 并直接获取您的数据如下

JSONObject store = json.getJSONObject(0);
于 2012-10-04T06:36:00.797 回答