【发布时间】:2017-04-03 10:34:05
【问题描述】:
大家好,我正在尝试使用 Volley 在 android 中实现 Login\Register 我使用 JSONObject 和 Repose.listener,当我在听众之外时,一切都是好的广告展示here
但我“在”侦听器“内部”密码字段“缺失”,如 here 所示
登录活动
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} 总是
附言 寄存器的工作方式相同(或多或少)并且工作正常
【问题讨论】:
-
mysqli_stmt_bind_result($statement, $username, $password);您正在尝试绑定 3 个变量,但在 SELECT 中只选择了 2 个;仅此一项就失败了,在查询中使用mysqli_error($con)会让您对此有所了解。 -
永远不要存储纯文本密码!请使用 PHP 的 built-in functions 来处理密码安全问题。如果您使用的 PHP 版本低于 5.5,您可以使用
password_hash()compatibility pack。在散列之前,请确保您 don't escape passwords 或对它们使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。 -
@Fred-ii- 根据 PHP 手册 php.net/manual/en/mysqli-stmt.bind-result.php 你可以做到,而且我不会出错
-
@JayBlanchard 我知道,但首先基本的必须工作,然后你在它之上构建
-
@styx 够公平的。如果您没有从 php/mysql 收到错误,那么这是一个 Android 问题,我将无法为您提供帮助。