2016-10-07 71 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并返回。

希望它能帮助:)