0

I'm trying to create an app that involves login. But I getting a problem in the registration.

I tried change the php but I get the same mensagem.

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;
    }

The log.

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)

The odd thing is, when I put the url: "http://teste-app-lhama.epizy.com/controle/registrar.php" in the browser I get this result:"{"status":2,"message":"Falta de parametro obrigatorios"(missing mandatory params)}" Probably the result is return the right value, right?

I believe the problem is in my php inside this if "if(isset($input['username']) && isset($input['password']) && isset($input['email']))"

I was reading in the internet, and I found someone say that this error could be caused by the host. If that is true, what i should do?

2 Answers 2

1

do not forget to set header content type to json , so response consider as json not string :) :

<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

do not do this :

echo "Email existente";

it is string , not json.

Sign up to request clarification or add additional context in comments.

Comments

0

the error is here:

$inputJSON = file_get_contents('http://teste-app-lhama.epizy.com/controle/registrar.php');

should be:

$inputJSON = file_get_contents('php://input');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.