gpt4 book ai didi

java - 当 jsonResponse 从 mySQL-DB 获取空数据时没有任何反应

转载 作者:行者123 更新时间:2023-11-28 23:10:42 26 4
gpt4 key购买 nike

我只是 java/android 编程的新手。

我正在编写一个应用程序,用户可以在其中自行注册和登录。数据保存在在线 mysql-db 中。注册和登录工作正常。用户通过 session 保持登录状态。

即使从 mysql-db 中获取数据也能正常工作,但是当某些数据库字段响应“null”时会出现一个问题。

这是我正在使用的代码

    public class UserProfileSettingsFragment extends PreferenceFragment
{

SessionManager session;

@Override
public void onCreate(final Bundle savedInstanceState)
{
SharedPreferences prefs = this.getActivity().getSharedPreferences("JampSharedPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);

session = new SessionManager(this.getActivity().getApplicationContext());



HashMap<String,String> user = session.getUserDetails();
final String sessionUsername = user.get(SessionManager.KEY_USERNAME);

// ResponseListener um Request Nutzerdaten auszulesen.
Response.Listener<String> UserDataResponseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");

// Wenn Datenabfrage erfolgreich, JSONResponse auswerten.
if (success) {
String responseRealName = jsonResponse.getString("realname");
String responseStreetName = jsonResponse.getString("streetname");
int responsePostcode = jsonResponse.getInt ("postcode");
String responseCity = jsonResponse.getString("city");
String responseState = jsonResponse.getString("state");
int responseAge = jsonResponse.getInt ("age");
int responseIsPremium = jsonResponse.getInt ("isPremium"); // BOOLEAN

Preference prefUserData = (Preference) findPreference("preferencescreen_userdata");
prefUserData.setTitle(sessionUsername);
//prefUserData.setSummary(responseRealName+"\n"+responseStreetName+"\n"+responsePostcode + " " + responseCity);

Preference prefUsername = (Preference) findPreference("settings_username");
prefUsername.setTitle(sessionUsername);

Toast.makeText(getActivity(),sessionUsername, Toast.LENGTH_LONG);

if (responseIsPremium==1){
//ivPremiumIcon.setVisibility(View.VISIBLE);
}


}else{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Konnte Nutzerdaten nicht abrufen.")
.setNegativeButton("Nochmal",null)
.create()
.show();
}

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

}


};

// Request an userdatarequest.php senden
UserDataRequest userDataRequest = new UserDataRequest(sessionUsername, UserDataResponseListener);
RequestQueue queue = Volley.newRequestQueue(this.getActivity());
queue.add(userDataRequest);


}
}

PHP代码:

$con = mysqli_connect("localhost","web506","lalala","usr_web506_1");

$username = $_POST["username"];

$statement = mysqli_prepare($con,"SELECT * FROM user WHERE username = ?");

mysqli_stmt_bind_param($statement,"s",$username);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);


mysqli_stmt_bind_result($statement,
$userID,
$username,
$password,
$email,
$age,
$realname,
$streetname,
$postcode,
$city,
$state,
$isPremium,
$isLoggedIn);

$response = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $username;
$response["password"] = $password;
$response["email"] = $email;
$response["age"] = $age;
$response["realname"] = $realname;
$response["streetname"] = $streetname;
$response["postcode"] = $postcode;
$response["city"] = $city;
$response["state"] = $state;
$response["isPremium"] = $isPremium;
$response["isLoggedIn"] = $isLoggedIn;

}

echo json_encode($response);

?>

因此,当我获取用户数据时,我可以使用 Toast、更改 preference.summaries 或其他方式显示它们。但是,如果某些 mysql 条目为空/空,则什么也不会发生。该应用程序没有崩溃,但它似乎没有从 php 文件中获得“成功” boolean 值。线索是什么?

提前致谢。埃里克


我应该删除 $response["success"] = false; 吗?如果应用程序无法连接到数据库并且错误的 boolean 值到达我的应用程序,我通常会收到一条警报消息,所以我认为它就在那里。

当我在我的变量后面添加空格时,我知道它们的 DB-cells 是空的,然后 jsonresponse 提供一个“0”值作为字符串结果,如下所示:

      $response["realname"] = $realname+" ";
$response["streetname"] = $streetname+" ";
$response["postcode"] = $postcode+" ";
$response["city"] = $city+" ";
$response["state"] = $state+ " ";

在 TextView 中,它们是否逐行显示为“0”。

我是否必须在我的应用程序中解决这个问题,或者是否有一种简单的方法可以以某种方式过滤空单元格并跳到下一个单元格?

最佳答案

    $response["success"] = true;
$record_size = 0;

while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $username;
$response["password"] = $password;
$response["email"] = $email;
$response["age"] = $age;
$response["realname"] = $realname;
$response["streetname"] = $streetname;
$response["postcode"] = $postcode;
$response["city"] = $city;
$response["state"] = $state;
$response["isPremium"] = $isPremium;
$response["isLoggedIn"] = $isLoggedIn;
$record_size++;
}

$response["record_size"] = $record_size;

echo json_encode($response);

对于记录数,我使用 $record_size 变量,以便您了解记录。因为 $response["success"] = true;意味着您已成功获取数据库,对于记录,您可以使用 $response["record_size"].. 我希望它能帮助您..

关于java - 当 jsonResponse 从 mySQL-DB 获取空数据时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46068980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com