【发布时间】:2019-10-09 16:39:38
【问题描述】:
我正在尝试创建一个涉及登录的应用程序。但是我在注册时遇到了问题。
我尝试更改 php,但得到相同的信息。
private void registerUser() {
displayLoader();
JSONObject request = new JSONObject();
try {
request.put(KEY_USERNAME, username);
request.put(KEY_PASSWORD, password);
request.put(KEY_EMAIL, email);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsArrayRequest = new JsonObjectRequest
(Request.Method.POST, register_url, request, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pDialog.dismiss();
try {
if (response.getInt(KEY_STATUS) == 0) {
session.loginUser(username,email);
loadDashboard();
}else if(response.getInt(KEY_STATUS) == 1){
etUsername.setError("Usuario já usado");
etUsername.requestFocus();
}else if(response.getInt(KEY_STATUS) == 3){
etEmail.setError("Email já usado");
etEmail.requestFocus();
} else{
Toast.makeText(getApplicationContext(),
response.getString(KEY_MESSAGE), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
private RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}
<?php
$response = array();
include 'conectar_db.php';
include 'funcoes.php';
$inputJSON = file_get_contents('http://teste-app-lhama.epizy.com/controle/registrar.php');
$input = json_decode($inputJSON, TRUE);
if(isset($input['username']) && isset($input['password']) && isset($input['email'])){
$username = $input['username'];
$password = $input['password'];
$password = hashPass($password);
$email = $input['email'];
if(!userExists($username)){
if(!emailExists($email)){
$salt = getSalt();
$passwordHash = password_hash(concatPasswordWithSalt($password,$salt),PASSWORD_DEFAULT);
$insertQuery = "INSERT INTO Informacoes(username, email, password_hash, salt) VALUES (?,?,?,?)";
if($stmt = $con->prepare($insertQuery)){
$stmt->bind_param("ssss",$username,$email,$passwordHash,$salt);
$stmt->execute();
$response["status"] = 0;
$response["message"] = "Usuario criado com sucesso";
$stmt->close();
}
}
else{
$response["status"] = 3;
$response["message"] = "Email j? existe";
}
}
else{
$response["status"] = 1;
$response["message"] = "Usuario j? existe";
}
}
else{
$response["status"] = 2;
$response["message"] = "Falta de parametro obrigatorios";
}
echo json_encode($response);
?>
function userExists($username){
$query = "SELECT username FROM Informacoes WHERE username = ?";
global $con;
if($stmt = $con->prepare($query)){
$stmt->bind_param("s",$username);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows == 1){
$stmt->close();
echo "Usuario existente";
return true;
}
$stmt->close();
}
return false;
}
function emailExists($email){
$query = "SELECT email FROM Informacoes WHERE email = ?";
global $con;
if($stmt = $con->prepare($query)){
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows == 1){
$stmt->close();
echo "Email existente";
return true;
}
$stmt->close();
}
return false;
}
日志。
D/EGL_emulation: eglMakeCurrent: 0xa24b0920: ver 3 0 (tinfo 0xa4e099a0)
I/chatty: uid=10091(com.lhamaintergalatica.controlefinaceiro) RenderThread identical 6 lines
D/EGL_emulation: eglMakeCurrent: 0xa24b0920: ver 3 0 (tinfo 0xa4e099a0)
D/EGL_emulation: eglMakeCurrent: 0xa24b0920: ver 3 0 (tinfo 0xa4e099a0)
D/EGL_emulation: eglMakeCurrent: 0xa24b0920: ver 3 0 (tinfo 0xa4e099a0)
奇怪的是,当我在浏览器中输入 url:“http://teste-app-lhama.epizy.com/controle/registrar.php”时,我得到了这个结果:“{”status”:2,”message”:“Falta de parametro obrigatorios”(缺少强制参数) }" 结果应该是返回正确的值吧?
我相信问题出在我的 php 里面 if "if(isset($input['username']) && isset($input['password']) && isset($input['email'])) "
我在网上阅读,发现有人说这个错误可能是由主机引起的。如果是真的,我该怎么办?
【问题讨论】: