2016-10-07 68 views
2
client.post("http://10.0.2.2/project/process/selectalluser.php", new AsyncHttpResponseHandler() { 

     @Override 
     public void onSuccess(String response) { 
      Integer contacts = controller.getContactsCount(); 
      Log.d("Reading contacts: ", contacts+""); 
      System.out.println("onSuccess"); 
      JSONArray jsonArray = new JSONArray(response); 
      Log.d("Reading response: ", response.length()+""); 
      System.out.println(response); 
      Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onFailure(int statusCode, Throwable error, String content) { 
      System.out.println("onFailure"); 
      System.out.println(statusCode); 
      System.out.println(error); 
      System.out.println(content); 
      Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_LONG).show(); 
     } 
    }); 

然後我的PHP:字符串轉換成JSON在Android的

$stmt = $dbh->prepare("SELECT * FROM `admin_tbl`"); 
          if ($stmt->execute()) { 
      $basicinfo = array(); 
      if ($stmt->rowCount() > 0) { 
       while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
        $basicinfo[] = array(
         'email' => $selected_row['email'], 
         'username' => $selected_row['username'], 
         'password' => $selected_row['password'], 
         'fname' => $selected_row['fname'], 
         'mname' => $selected_row['mname'], 
         'lname' => $selected_row['lname'], 
         'suffix' => $selected_row['suffix'], 
         'status' => $selected_row['status']); 
       } 
       echo json_encode($basicinfo, JSON_UNESCAPED_UNICODE); 
       //return $basicinfo; 
      } else { 
       echo json_encode(array(
        'error' => false, 
        'message' => "No record found!" 
       )); 
       exit; 
      } 
     } 

這是我的代碼,我想這是字符串類型,以JSON的,所以我可以算多少條記錄那裏的響應轉換。但我得到的Unhandled Exception: org.json.JSONException錯誤行JSONObject json = new JSONObject(response);

System.out.println(response);將導致如下:

I /的System.out:[{ 「電子郵件」: 「[email protected]」, 「用戶名」: 「管理員」, 「密碼」: 「管理」,其中 「fname」: 「布拉德利」, 「MNAME」: 「Buenafe」, 「L-NAME」: 「Dalina」, 「後綴」: 「」, 「狀態」: 「1」 }]

如何字符串格式正確轉換成JSON

回答

0

你的迴應不是JSONObject但是JSONArray,[]表示數組。一個普通的對象不會有這些。您可以使用

JSONArray jsonArray = new JSONArray(response); 
+0

我需要有一個特定的進口對於我不是能提到這一點,但我想這已經 –

+0

'org.json.JSONArray'? –

+0

我認爲不會有像上面那樣的錯誤 –

0

相反的:

JSONObject json = new JSONObject(response); 

用途:

JSONArray json = new JSONArray(response); 

當您在System.out中所顯示的,你得到的響應是JSON陣列而不是JSON目的。

JSON數組總是以「[」和JSON對象始終開頭「{」開始

+0

未來PHP頁面的問題做我需要爲我不是能提這個特定的進口,但我想作爲的JSONObject即'這已經 –

+0

同類進口org.json.JSONArray' 。如果你仍然面臨這個問題,請張貼最新的日誌,因爲按照舊的日誌,這應該可以解決問題。 –

0
try { 
    JSONArray jsonArray = new JSONArray(response); 
    Log.d("Reading response: ", jsonArray.length()+""); 
} catch (JSONException e) { 
    // Do something with the exception 
} 

我讀的地方,你需要把在嘗試捕捉。我做了,它沒有錯誤,並且返回了正確的長度。

我不確定這是否是解決此問題的方法。

+0

這並沒有什麼意義 –

+0

你是什麼意思,現在我正在使用try catch解決方案是否有任何理由會失敗?@NielsMasdorp –

+0

那麼,try catch只捕獲'Exception'來防止你的應用程序崩潰,但它不會阻止拋出實際的Exception。 –

0

我對JSON瞭解不多,但是當我不得不使用它時,我調查了一下google的github模式。 Gson - https://github.com/google/gson您只需要獲取庫並添加Maven依賴項。

一旦你這樣做了,你可以使用toJson()和fromJson()將Java對象轉換爲JSON並返回。

希望它能幫助:)