gpt4 book ai didi

php - responseLitsner 中缺少字段 - Android Studio

转载 作者:行者123 更新时间:2023-11-29 19:56:01 25 4
gpt4 key购买 nike

大家好,我正在尝试使用 Volley 在 android 中实现登录\注册我使用 JSONObject 和 Repose.listener,当我在监听器之外时,一切都很好,广告显示 here

但在监听器“内部”,密码字段“丢失”,如图所示 here

登录 Activity

package com.example.ofir.bopofinal.LoginRegister;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.example.ofir.bopofinal.MainActivity;
import com.example.ofir.bopofinal.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

final EditText etUserName = (EditText) findViewById(R.id.etUserName);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);

registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent regiserIntent = new Intent(LoginActivity.this,RegisterActivity.class);
LoginActivity.this.startActivity(regiserIntent);
}
});

bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String username = etUserName.getText().toString();
final String password = etPassword.getText().toString();

Response.Listener<String> responseLitsner = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success){
String name = jsonResponse.getString("name");
int age = jsonResponse.getInt("age");

Intent loginIntent = new Intent(LoginActivity.this, MainActivity.class);
loginIntent.putExtra("name", name);
loginIntent.putExtra("username", username);
loginIntent.putExtra("age", age);
LoginActivity.this.startActivity(loginIntent);

}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};

LoginRequest loginRequest = new LoginRequest(username,password, responseLitsner);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}

登录请求

package com.example.ofir.bopofinal.LoginRegister;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
* Created by ofir on 11/17/2016.
*/
public class LoginRequest extends StringRequest {


private static final String REGISTER_REQUEST_URL = "http://cellularguide.info/cellularguide.info/offir/Login.php";
private Map<String,String> params;

public LoginRequest(String username ,String password, Response.Listener<String> listener){
super(Method.POST,REGISTER_REQUEST_URL,listener,null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);

}
@Override
public Map<String, String> getParams() {
return params;
}
}

登录.PHP

<?php
$con = mysqli_connect(....); // the con is working - dont worry

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT username,password FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $username, $password);

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

while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["age"] = $age;
$response["username"] = $username;
$response["password"] = $password;
}

echo json_encode($response);
?>

此原因响应:{"success":false} 始终

附注寄存器的工作方式相同(或多或少)并且工作正常

最佳答案

我看到您在调用登录之前调用了响应。它从哪里来?您似乎在 bLogin.setOnClickListener 中发送请求 (LoginRequest) 之前调用了响应 (Response.Listener)。请尝试扭转它们。

关于php - responseLitsner 中缺少字段 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696321/

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