JSONException没有价值

时间:2014-05-27 05:57:10

标签: java android json

我已经创建了以下价格和移动型号名称的json数组。此数据从wamp phpmyadmin服务器读取,表名为"产品":

    {
    "products": [
        {
            "pid": "14",
            "name": "zxc",
            "price": "123456"
        },
        {
            "pid": "6",
            "name": "Sony    Xperia",
            "price": "35000"
        },
        {
            "pid": "8",
            "name": "Samsung Galaxy Note",
            "price": "32000"
        },
        {
            "pid": "5",
            "name": "htc",
            "price": "26326"
        },
        {
            "pid": "9",
            "name": "Nokia Lumia 800",
            "price": "18000"
        },
        {
            "pid": "2",
            "name": "iphone",
            "price": "12345"
        },
        {
            "pid": "15",
            "name": "sdjnas",
            "price": "12243"
        },
        {
            "pid": "13",
            "name": "Samsung S5222",
            "price": "6500"
        },
        {
            "pid": "11",
            "name": "Nokia C201",
            "price": "4400"
        },
        {
            "pid": "7",
            "name": "Nokia Asha 200",
            "price": "4000"
        },
        {
            "pid": "1",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "3",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "4",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "10",
            "name": "aks",
            "price": "1234"
        },
        {
            "pid": "12",
            "name": "asd",
            "price": "123"
        }
    ],
    "success": 1
}

我对以下代码块有疑问:

    try 
    {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1)
        {
                products = json.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++)
                {
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                }
        }

            else
            {
                Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
    }

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

标签是简单的字符串值:

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

每当我运行此代码时,它会在logcat中显示它显示JSONException没有成功的值。我无法弄清楚究竟是什么导致了这个问题。先感谢您。

2 个答案:

答案 0 :(得分:1)

检查:

JSONObject json = jParser.makeHttpRequest(URL,
                    POST, params);

从那里How to httpPost() to covert httpGet()

答案 1 :(得分:0)

请检查一下,

用于获取JSON getJSONFromUrl()

的方法

HttpPost更改为HttpGet

请参阅此Link