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");

ストアは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

したがって、jsonをとしてJSONObject宣言する代わりにJSONArray

次に、storeObjを削除して、次のようにデータを直接取得します

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