JSON中没有这样的值错误

时间:2014-03-12 05:37:16

标签: android

在我的应用程序中,我的后端是PHP,我正在以 JSON 的形式获取值,我正在点击一些xyz url并且它提供了一些JSON对象,例如说10个Json对象(即。从0到10个Json对象),并且当用户按下该按钮时我有load_more按钮,然后将击中相同的xyz url并获得另外10个对象(即从10到20个Json对象), 现在问题是它第一次占用单个Json对象中的所有值但是如果我第二次点击它没有获取单个Json对象中的所有值 即使值可用,也说没有找到这样的值

这是我的Asynctask

class MyTask extends AsyncTask<String, Void, String> {

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(Gropinion_Questions.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        try {
            pDialog.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(String... params) {
        return Utils.getJSONString(params[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);


        if (null == result || result.length() == 0) {
            showToast("No data found from web!!!");
            Gropinion_Questions.this.finish();
        } else {

            // get JSON result and give to JsonObject
            try {
                JSONObject mainJson = new JSONObject(result);
                System.out.println("result........."+result);

                JSONArray    jsonArray = mainJson.getJSONArray(ARRAY_NAME);

                System.out.println("@@@@@@@@@@ jsonArray @@@@@@@@@@@ "+jsonArray.length());

                Singleton.jsonArray = jsonArray;
                for (int i = 0; i < 10; i++) {

                    JSONObject objJson = jsonArray.getJSONObject(i);

                    objItem = new QuestionsObject();

                    objItem.setDbcsFacebookProfile(objJson.optString(dbcsFacebookProfile));

                    objItem.setAvatar_img(objJson.optString(Avatar_image));

                    objItem.setVotebar(objJson.optString(percentagenumber));

                    objItem.setProfile_picture_value(objJson.optString(PROFILE_PIC_VALUE));

                    objItem.setId(objJson.optString(ID));
                    objItem.setTitle(objJson.optString(TITLE));
                    objItem.setNick_name(objJson.optString(NICKNAME));
                    objItem.setQues_type(objJson.optString(QUESTIONTYPE));
                    objItem.setCategories(objJson.getString(CATEGORIES));
                    objItem.setSubcatid(objJson.getString(SUBCATID));
                    objItem.setTotal_vote(objJson.getString(TV));

                    arrayOfList.add(objItem);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            setAdapterToListview();

            if (null != pDialog && pDialog.isShowing()) {
                try {
                    pDialog.dismiss();
                    pDialog = null;
                } catch (Exception e) {
                }
            }
        }
    }
}

这是我的第一个Json回复

 {
    "questions": [
        {
            "ID": "4260",
            "unique_que": "N2pJS",
            "Title": "Which actress looks more beautiful?",
            "NickName": "shon17",
            "Avatar": "0",
            "dbcsFacebookProfile": "0",
            "QuestionType": "1",
            "Categories": "Shopping & Fashion",
            "SubCategoryID": "5",
            "Hash": "531ff16d876fe",
            "Ext": "jpg",
            "avragequestionrate": "0",
            "progressBar": "0, 0, 0, 0",
            "TotalVote": "0",
            "productImage": "531ff16d876fe.jpg,531ff16d89841.jpg,,",
            "description": "Katrina,kareena,,",
            "productThumbImage": "531ff16d876fe_thumb_75_75.png,531ff16d89841_thumb_75_75.png,,",
            "avatarImage": "1258602873_avatar_75_75.jpg",
            "Default_image_status": "1",
            "voteBar": "0, 531ff16d876fe.jpg, 531ff16d876fe_thumb_75_75.png"
        }

这是我的第二个Json回复

 {
    "questions": [
        {
            "ID": "4232",
            "unique_que": "b6h4a",
            "Title": "which actress do u like more?",
            "NickName": "shon17",
            "Avatar": "0",
            "dbcsFacebookProfile": "0",
            "QuestionType": "1",
            "Categories": "Shopping & Fashion",
            "SubCategoryID": "5",
            "Hash": "531eb4fdc0dd3",
            "Ext": "jpg",
            "avragequestionrate": "0",
            "progressBar": "33, 1, 66, 2, 0, 0, 0, 0, 3",
            "TotalVote": "3",
            "productImage": "531eb4fdc0dd3.jpg,531eb4fdc2c4c.jpg,,",
            "description": "katrina,deepika,,"
        }

这就是我作为回应的方式,但是当我在浏览器中点击它时,它显示了load_more的所有字段,但作为响应它没有提取

我从getstring更改为optstring现在它说不同的exceotion

这是我的例外

   03-12 06:09:03.705: W/System.err(1408): org.json.JSONException: Index 7 out of range [0..7)
03-12 06:09:03.705: W/System.err(1408):     at org.json.JSONArray.get(JSONArray.java:263)
03-12 06:09:03.705: W/System.err(1408):     at org.json.JSONArray.getJSONObject(JSONArray.java:480)
03-12 06:09:03.705: W/System.err(1408):     at com.android.groupinion.Gropinion_Questions$MyTask.onPostExecute(Gropinion_Questions.java:1041)
03-12 06:09:03.705: W/System.err(1408):     at com.android.groupinion.Gropinion_Questions$MyTask.onPostExecute(Gropinion_Questions.java:1)
03-12 06:09:03.705: W/System.err(1408):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-12 06:09:03.705: W/System.err(1408):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-12 06:09:03.705: W/System.err(1408):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-12 06:09:03.705: W/System.err(1408):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 06:09:03.705: W/System.err(1408):     at android.os.Looper.loop(Looper.java:137)
03-12 06:09:03.705: W/System.err(1408):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-12 06:09:03.705: W/System.err(1408):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 06:09:03.705: W/System.err(1408):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 06:09:03.705: W/System.err(1408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-12 06:09:03.705: W/System.err(1408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 06:09:03.705: W/System.err(1408):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

据我了解你。如果没有带有传递给get方法的键的值,则会有异常。

因此,您可以使用optJSONArray来获取数组,并选择opt(如optString,optJSONObject等)方法来获取其他值。这些方法不会抛出异常,如果没有传递密钥的值,它们只返回null。

您获得异常是因为json数组长度小于10因此最好使用 JSONArray对象的方法length()而不是数字10 for。

这样的事情:

for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject objJson = jsonArray.getJSONObject(i);

    objItem = new QuestionsObject();

    objItem.setDbcsFacebookProfile(objJson.optString(dbcsFacebookProfile));

    objItem.setAvatar_img(objJson.optString(Avatar_image));

    objItem.setVotebar(objJson.optString(percentagenumber));

    objItem.setProfile_picture_value(objJson.optString(PROFILE_PIC_VALUE));

    objItem.setId(objJson.optString(ID));
    objItem.setTitle(objJson.optString(TITLE));
    objItem.setNick_name(objJson.optString(NICKNAME));
    objItem.setQues_type(objJson.optString(QUESTIONTYPE));
    objItem.setCategories(objJson.getString(CATEGORIES));
    objItem.setSubcatid(objJson.getString(SUBCATID));
    objItem.setTotal_vote(objJson.getString(TV));

    arrayOfList.add(objItem);
}

答案 1 :(得分:0)

试试这个..

除了以下三个

之外,你所做的一切都是正确的
objItem.setCategories(objJson.getString(CATEGORIES));
objItem.setSubcatid(objJson.getString(SUBCATID));
objItem.setTotal_vote(objJson.getString(TV));

将其getString更改为optString

objItem.setCategories(objJson.optString(CATEGORIES));
objItem.setSubcatid(objJson.optString(SUBCATID));
objItem.setTotal_vote(objJson.optString(TV));

或者

if(objJson.has(CATEGORIES))
   objItem.setCategories(objJson.getString(CATEGORIES));

if(objJson.has(SUBCATID))
   objItem.setSubcatid(objJson.getString(SUBCATID));

if(objJson.has(TV))
   objItem.setTotal_vote(objJson.getString(TV));

修改

如下所示更改您的for,您已指定 10 ,但jsonArray长度仅为7,该错误索引超出范围

for (int i = 0; i < jsonArray.length(); i++) {

答案 2 :(得分:0)

请通过此循环更新您的代码:

for (int i = 0; i < 10; i++) {
    JSONObject objJson = jsonArray.getJSONObject(i);
    objItem = new QuestionsObject(); 
    objItem.setDbcsFacebookProfile(objJson.optString(dbcsFacebookProfile));
    objItem.setAvatar_img(objJson.optString(Avatar_image));   
    objItem.setVotebar(objJson.optString(percentagenumber));   
    objItem.setProfile_picture_value(objJson.optString(PROFILE_PIC_VALUE));   
    objItem.setId(objJson.optString(ID));
    objItem.setTitle(objJson.optString(TITLE));
    objItem.setNick_name(objJson.optString(NICKNAME));
    objItem.setQues_type(objJson.optString(QUESTIONTYPE));
    objItem.setCategories(objJson.getString(CATEGORIES));
    objItem.setSubcatid(objJson.getString(SUBCATID));   
    if(objJson.has(TV)){       
        objItem.setTotal_vote(objJson.getString(TV));      
    }
    arrayOfList.add(objItem);
}