0

.Java file. This is where error is---> int success = json.getInt(TAG_SUCCESS);

       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
            **int success = json.getInt(TAG_SUCCESS);**

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);
                Log.d("level1: ", "@@@@@@@@@@@@@@@@@@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    Log.d("level2: ", "lksdjflsdjf0wrewrwje************************");
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Log.d("level3: ", "jldksffffffffffffffffffffffffffffffffffffff");
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

Json Array from server is below. I have validated it from jsonlint.com

     {
"tbl_user": {
    "0": {
        "id": "195",
        "email": "aru@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "aru",
        "lname": "sharma"
    },
    "1": {
        "id": "196",
        "email": "manu@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "manu",
        "lname": "sharma"
    },
    "2": {
        "id": "197",
        "email": "rishi@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "rishi",
        "lname": "sharma"
    },
    "success": 1
}

}

I think I am unable to read success value from here. I am unable to see the error in this Json string. Please help.

Rest Php code on server creating json is

    function getUsers() {
$sql = "select * FROM tbl_user ORDER BY fname";
try {
    $db = getConnection();
    $stmt = $db->query($sql);  
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);
    $users["success"] = 1;
    $db = null;
    echo '{"tbl_user": ' . json_encode($users) . '}';

} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

}

vikas sharma
  • 161
  • 4
  • 13
  • Is it returns int or string , are you sure it returning int. check that first – RajaReddy PolamReddy Jan 24 '13 at 03:40
  • if you look in to my json array from server. it returns "success":1, which is an int. – vikas sharma Jan 24 '13 at 03:53
  • I have already done it like that. 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 **int success = json.getInt(TAG_SUCCESS);** – vikas sharma Jan 24 '13 at 04:07
  • I think issue is with how I have formed the json string at server. I am unable to read int success = json.getInt(TAG_SUCCESS); from json – vikas sharma Jan 24 '13 at 04:13
  • Spending some more time with json. I noticed my json string should look like "3": { "success": 1 } not "success": 1. Please let me know how can i change it? – vikas sharma Jan 24 '13 at 04:54

2 Answers2

0

try like this, we have to get JSON object first and then inner values in that.

JSONObject json = new JSONObject(result);
int success =   json.getInt(TAG_SUCCESS);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

Try this:

PHP:

try {
$db = getConnection();
$stmt = $db->query($sql);  
$users["tbl_user"] = $stmt->fetchAll(PDO::FETCH_OBJ);
$users["success"] = 1;
$db = null;
echo json_encode($users) 

} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

and in java file:

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
int success = json.getInt(TAG_SUCCESS);
SKK
  • 5,261
  • 3
  • 27
  • 39
  • just a note, whenever u have problems with php code, check error_log in the server path. let me know if this didn't solve your issue. – SKK Jan 24 '13 at 04:57
  • Thanks a lot Santhosh. I modified the PHP code the way you mentioned. It has worked. I did not use your java suggestion..Thanks. Can you please briefly tell me what was the issue? – vikas sharma Jan 24 '13 at 05:18
  • There is no change in the java side. The issue was as you suspected - the formation of json in the server code. – SKK Jan 24 '13 at 05:35