我正在尝试创建一个涉及登录的应用程序。但是我在注册时遇到了问题。
我尝试更改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(isset($ input ['username'])&& isset($ input ['password'])&& isset($ input ['email'])) “
我正在互联网上阅读,我发现有人说此错误可能是由主机引起的。如果是这样,我该怎么办?
答案 0 :(得分:1)
不要忘记将标头内容类型设置为json,因此响应应视为json而不是string :):
<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);
不要这样做:
echo "Email existente";
它是字符串,而不是json。
答案 1 :(得分:0)
错误在这里:
$inputJSON = file_get_contents('http://teste-app-lhama.epizy.com/controle/registrar.php');
应为:
$inputJSON = file_get_contents('php://input');