2013-01-24 27 views
0

.Java文件。這是錯誤是---> int success = json.getInt(TAG_SUCCESS);從服務器JSONException錯誤:其餘的php服務器-Android客戶端

 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數組如下。我從jsonlint.com

 { 
"tbl_user": { 
    "0": { 
     "id": "195", 
     "email": "[email protected]", 
     "password": "202cb962ac59075b964b07152d234b70", 
     "fname": "aru", 
     "lname": "sharma" 
    }, 
    "1": { 
     "id": "196", 
     "email": "[email protected]", 
     "password": "202cb962ac59075b964b07152d234b70", 
     "fname": "manu", 
     "lname": "sharma" 
    }, 
    "2": { 
     "id": "197", 
     "email": "[email protected]", 
     "password": "202cb962ac59075b964b07152d234b70", 
     "fname": "rishi", 
     "lname": "sharma" 
    }, 
    "success": 1 
} 

}

我覺得我無法從這裏讀取成功值驗證它。我無法看到這個Json字符串中的錯誤。請幫忙。在服務器上創建JSON

休息PHP代碼是

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() .'}}'; 
} 

}

+0

是它返回int或字符串,你確定它返回int。如果你從服務器上查看我的json數組,請檢查第一個 –

+0

。它返回「成功」:1,這是一個int。 –

+0

我已經這樣做了。 JSONObject json = jParser.makeHttpRequest(url_all_products,「GET」,params); //檢查你的日誌貓的JSON響應 Log.d(「All Products:」,json.toString()); 嘗試{ //檢查成功標籤 ** int success = json.getInt(TAG_SUCCESS); ** –

回答

0

試試這個:

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() .'}}'; 
} 

和Java文件:

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 
int success = json.getInt(TAG_SUCCESS); 
+0

只是一個說明,只要你有php代碼的問題,請檢查服務器路徑中的error_log。讓我知道如果這沒有解決你的問題。 – SKK

+0

非常感謝Santhosh。我按照你提到的方式修改了PHP代碼。它工作。我沒有使用你的Java建議..謝謝。你能否簡單地告訴我是什麼問題? –

+0

在java方面沒有改變。這個問題是你懷疑的 - 在服務器代碼中形成json。 – SKK

0

嘗試這樣的,我們必須先獲得JSON對象是內在價值。

JSONObject json = new JSONObject(result); 
int success = json.getInt(TAG_SUCCESS);