0
我想將登錄用戶存儲在新的會話中,從數據庫獲取信息,將其放入json數組,然後在android中獲取數組在佈局中顯示結果。但是,當我運行應用程序時,從php文件返回的數組是空的。如果我在不同的程序,如郵差但是測試php文件,我得到正確的響應
{"result":[{"id":"12","fname":"C","lname":"Wei","username":"cw","email":"werrs.com"}]}
這裏是php文件和Java方法。
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'GET'){
$username = $_SESSION['username'];
$sql = "SELECT * FROM USER WHERE username = '$username'";
require_once('connect.inc.php');
$run = mysqli_query($conn, $sql);
$result = array();
while($row = mysqli_fetch_array($run)){
array_push($result, array(
'id' => $row['id'],
'fname' => $row['fname'],
'lname' => $row['lname'],
'username' => $row['username'],
'email' => $row['email'],
));
}
echo json_encode(array('result'=>$result));
mysqli_close($conn);
}
?>
在郵差,它成功地獲取登錄的用戶,但在Android它不。
private void getUserData() { StringRequest stringRequest = new
StringRequest(Request.Method.GET,
Config.SERVER_ADDRESS + "GetUserData.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject jsonObject = null;
try {
//json string to jsonobject
jsonObject = new JSONObject(response);
//get json sstring created in php and store to JSON Array
result = jsonObject.getJSONArray(Config.json_array);
//get username from json array
getUserInfo(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void getUserInfo(JSONArray jsonArray){
for(int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject json = jsonArray.getJSONObject(i);
userInfo.add(json.getString(Config.getUsername));
} catch (JSONException e) {
}
}
}
我使用散列表來做這個嗎? – cdub
只需以最適合您的需求的格式存儲以前獲取的會話標識並將其附加到以下請求:Config.SERVER_ADDRESS +「GetUserData.php?PHPSESSID =」+ myPreviouslyStoredSessionId –
好的。我正在努力獲取會話ID。 – cdub