2012-01-08 21 views
0

所以有一個PHP文件,我發送數組到我的Android設備。在我的設備接收到數據後,它將它轉換爲一個字符串,之後我試圖獲取由PHP創建的assoc數組索引。這裏是PHP代碼從之前的樣本:JSONException,而試圖將數據轉換爲字符串

if(mysql_num_rows($query)){ 
    $output = array('message' => 'Group added to your database', 'succes' => 'true'); 

}else{ 
    $output = array('message' => 'Wrong username/password combination', 'succes' => 'false'); 
} 

print(json_encode($output)); 

,這裏是我的Android項目中的代碼看起來像:

protected void onPostExecute(String result) { 

     dialog.dismiss(); 

     try{ 
      jArray = new JSONArray(result); 
      JSONObject json_data=null; 

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

       json_data    = jArray.getJSONObject(i);  

       String getMessage = json_data.getString("message"); 
       showToast(getMessage); 
      } 
     } 
     catch(JSONException je){ 
      Log.e("log_tag", "Error (JSONException): "+je.toString()); 
      showToast(result); 
     } 
     catch (ParseException pe) { 
      pe.printStackTrace(); 
     } 
    } 

錯誤日誌:

01-08 16:35:55.896: E/log_tag(647): Error (JSONException): org.json.JSONException: Value {"message":"Group added to your database","success":"true"} of type org.json.JSONObject cannot be converted to JSONArray 
01-08 16:35:56.186: W/TextLayoutCache(647): computeValuesWithHarfbuzz -- need to force to single run 
+0

Json異常中提供了哪些細節?成功的拼寫也是成功的。 – 2012-01-08 10:51:48

+0

錯誤(JSONException):org.json.JSONException:值{「消息」:「組添加到您的數據庫」,「成功」:「true」}的類型org.json.JSONObject不能轉換爲JSONArray – Xarialon 2012-01-08 15:42:04

回答

1

你是試圖從表示JSONObject的字符串創建JSONArray,您應該這樣做:

try{ 
    JSONObject json_data=JSONObject(result); 
    String getMessage = json_data.getString("message"); 
    showToast(getMessage); 
} 
+0

是啊,我看到也是如此,但並不知道如何解決問題。謝謝:) – Xarialon 2012-01-08 15:51:33