2016-01-06 33 views
1

編輯:20020-20020/practise.mysql_php_json I/System.out的:org.json.JSONException:java.lang.String類型的值BR不能轉換到JSONObjectww 是我正在收到的終端消息。沒有得到與Android應用JSON響應

我工作得到一個簡單的插入和檢索應用程序在Android上工作,遵循YouTube上的教程。應該相當簡單,我可以將用戶信息發佈到在線數據庫,但是當我去檢索它時,沒有任何反應。沒有得到任何錯誤,只是沒有得到任何響應在JSON中。

我剛剛開始使用JSON和PHP兩種工具,所以我不知道我是不是在找東西或缺少一些小東西。

這是我的connection.php我知道我的憑據是正確的,因爲我可以插入。

<?php 

    define('hostname', '--------'); 
    define('user', '---------'); 
    define('password', '-------'); 
    define('database', '------------------'); 

    $connect = mysqli_connect(hostname, user, password, database); 

?> 

這裏是我的insertUser.php

<?php 

if($_SERVER["REQUEST_METHOD"]=="POST") { 
    require 'connection.php'; 
    createUser(); 
} 

function createUser() { 
    global $connect; 

    $name = $_POST["name"]; 
    $email = $_POST["email"]; 
    $password = $_POST["password"]; 

    $query = " Insert into user_table(user_name, user_email, user_password) values ('$name','$email','$password');"; 

    mysqli_query($connect, $query) or die (mysqli_error($connect)); 
    mysqli_close($connect); 
} 

?> 

這裏是我的showUsers.php

<?php 

if($_SERVER["REQUEST_METHOD"]=="GET"){ 
    include 'connection.php'; 
    showStudent(); 
} 
function showStudent() 
{ 
    global $connect; 

    $query = " Select * FROM users; "; 

    $result = mysqli_query($connect, $query); 
    $number_of_rows = mysqli_num_rows($result); 

    $temp_array = array(); 

    if($number_of_rows > 0) { 
     while ($row = mysqli_fetch_assoc($result)) { 
      $temp_array[] = $row; 
     } 
    } 

    header('Content-Type: application/json'); 
    echo json_encode(array("users"=>$temp_array)); 
    mysqli_close($connect); 

} 

?> 

現在,這裏是在主要活動我的Android應用程序代碼。這應該只是得到我的數據庫中用戶的姓名,電子郵件和密碼。我有另一個onclick方法來插入用戶。

showButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     JsonObjectRequest jsonObjectRequest; = new JsonObjectRequest(Request.Method.GET, showURL, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONArray users = response.getJSONArray("users"); 
        for(int i=0; i<users.length(); i++) { 
         JSONObject user = users.getJSONObject(i); 
         String name = user.getString("user_name"); 
         String email = user.getString("user_email"); 
         String password = user.getString("user_password"); 

         results.append(name); 
        } 

        results.append("\n"); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 

     requestQueue.add(jsonObjectRequest); 
    } 
}); 
+0

在瀏覽器中調用showUsers.php時會得到什麼?任何錯誤? – Jeff

+0

嘗試設置內容類型:http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script –

+0

傑夫,沒有得到任何錯誤。沒有任何東西顯示在屏幕上。雖然我只注意到在日誌貓我得到20020-20020/practise.mysql_php_json I/System.out:org.json.JSONException:類型java.lang.String的值不能轉換爲JSONObjectww – ZWis212

回答

0

你的php腳本出現錯誤。第一步是打開你的錯誤報告,所以把下面右邊的代碼你開下<?php標籤:

ini_set('display_errors', 1); 
error_reporting(-1); 

會告訴你的錯誤。現在閱讀評論,你的查詢似乎失敗了。

在嘗試獲取結果之前,您應該檢查查詢以確保它確實成功。

$query = " Select * FROM users; "; 

$result = mysqli_query($connect, $query); 
// this checks if query failed and prints error. 
if(!$result){ 
    die("SQL Error: ". mysqli_error($connect)); 
} 
// will only get here if your query runs successfully. 
$number_of_rows = mysqli_num_rows($result); 
///... rest of your code... 

現在,讓我們知道你得到什麼錯誤,如果你設法解決它們。我會在回覆時更新此答案。

0

我寧願說這可能是你的查詢錯誤。奇怪的是,你插入用戶到user_table,然後以某種方式從users中選擇。所以如果你真的想顯示用戶不應該至少從同一張表中選擇?反正你可以修改代碼,如:

insertUser.php

<?php 
error_reporting(E_ALL); 
try{ 
    $handler = new PDO('mysql:host=localhost;dbname=database', 'username', 'password'); 
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
}catch(Exception $e){ 
    echo $e->getMessage(); 
    die(); 
} 

$name = $_POST["name"]; 
$email = $_POST["email"]; 
$password = $_POST["password"]; 
$query = $handler->query("INSERT INTO user_table(user_name, user_email, user_password) VALUES('$name','$email','$password')"); 

?> 

取出的mysqli連接,並使用PDO,而不是也被PHP本身建議。

showUsers。PHP

<?php 
error_reporting(E_ALL); 
try{ 
    $handler = new PDO('mysql:host=localhost;dbname=database', 'username', 'password'); 
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
}catch(Exception $e){ 
    echo $e->getMessage(); 
    die(); 
} 

$query = $handler->query("SELECT * FROM user_table"); 
$json = array(); 
$json = $query->fetchAll(PDO::FETCH_ASSOC); 
$records['users'] = $json; 
echo json_encode($records); 
?> 

你的結果將是一個JSON陣列稱爲用戶,然後你可以從Java中的數據。

希望它有幫助!