0

This is my register code for the app the error i got doesnt show where is the problem it just says timeout error when i press the button register

package com.example.registerlogindemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class Register extends AppCompatActivity {
    private EditText etName, etEmail, etPassword, etReenterPassword;
    private TextView tvStatus;
    private Button btnRegister;
    private String URL = "http://192.168.0.128/senior2/signup.php";
    private String name, email, password, reenterPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        etName = findViewById(R.id.etName);
        etEmail = findViewById(R.id.etEmail);
        etPassword = findViewById(R.id.etPassword);
        etReenterPassword = findViewById(R.id.etReenterPassword);
        tvStatus = findViewById(R.id.tvStatus);
        btnRegister = findViewById(R.id.btnRegister);
        name = email = password = reenterPassword = "";
    }

    public void save(View view) {
        name = etName.getText().toString().trim();
        email = etEmail.getText().toString().trim();
        password = etPassword.getText().toString().trim();
        reenterPassword = etReenterPassword.getText().toString().trim();
        if(!password.equals(reenterPassword)){
            Toast.makeText(this, "Password Mismatch", Toast.LENGTH_SHORT).show();
        }
        else if(!name.equals("") && !email.equals("") && !password.equals("")){
            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.equals("success")) {
                        tvStatus.setText("Successfully registered.");
                        btnRegister.setClickable(false);
                    } else if (response.equals("failure")) {
                        tvStatus.setText("Something went wrong!");                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.toString().trim(), Toast.LENGTH_SHORT).show();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> data = new HashMap<>();
                    data.put("name", name);
                    data.put("email", email);
                    data.put("password", password);
                    return data;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            requestQueue.add(stringRequest);
        }
    }

    public void login(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

This is my php signup that i created and it is working i tested it

<?php
$con = new mysqli("localhost", "root", "Aisha55733441", "signupdb", 3307);

$st_check = $con->prepare("SELECT * FROM users WHERE email = ?");
$st_check->bind_param("s", $_GET["email"]);
$st_check->execute();
$rs = $st_check->get_result();

if ($rs->num_rows == 0) {
    $st = $con->prepare("INSERT INTO users (id, username, email, password) VALUES (?, ?, ?, ?)");
    if (!$st) {
        die('Invalid query: ' . $con->error);
    }
    $st->bind_param("isss",$_GET["id"], $_GET["username"], $_GET["email"],$_GET["password"]);

    if (!$st->execute()) {
        die('Invalid query: ' . $st->error);
    }
    echo "successful";
} else {
    $user = $rs->fetch_assoc();
    echo "User with ID " . $user["id"] . " already exists";
}

I searched everywhere and i couldnt find the error cause so please if anybody can help

i want the user information to be inserted here

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Does `http://192.168.0.128/senior2/signup.php` time out if you access it a different way (e.g. via a browser)? If so then the problem is likely to be in the PHP or webserver. If not, then maybe it's a connection error between the Android app and your server. I notice you're using a local LAN IP address...is your Android device connected to the same LAN? – ADyson May 17 '23 at 09:35
  • P.S. It's not very secure to send a password in a URL GET parameter...it can easily by logged by webservers, routers etc. Better to send a POST request with the data in the body, and use HTTPS. – ADyson May 17 '23 at 09:37
  • Also, please don't store passwords in plain text - that is another security risk. Learn about [password hashing](https://www.php.net/manual/en/faq.passwords.php) instead. See also [How to use PHP's password_hash to hash and verify passwords](https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords) – ADyson May 17 '23 at 09:37
  • And please bring your error handling into the 21st century. Add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before your `mysqli_connect()` (or `new mysqli()`) command, and this will ensure that errors with your SQL queries are reported correctly to PHP automatically. That way you don't need to clutter your script with repetitive code to keep checking errors after every mysqli command. And you should never be echoing error data deliberately - it can easily reveal sensitive info to attackers by accident. – ADyson May 17 '23 at 09:38
  • 1
    Notice how you are sending certain data from Android: `data.put("name", name);` and how do you try to retrieve them on the server: `$_GET["username"]` Also, you never send `id`, but you try to retrieve it: `$_GET["id"]`. Also, your style of programming in PHP is "naive", too optimistic, you don't check that the values exist in `$_GET`. One other thing, in Android you said you will use `POST`, why do you use `$_GET` on the server and not `$_POST`? – A. Cedano May 17 '23 at 12:35
  • Agree with the above...on closer inspection this does appear to be two pieces of separate code, not particularly designed to work together, but thrown together on the basis of...what? Guesswork, perhaps? It's unclear _why_ you ever expected this to be correct? Programming is not guesswork. Learn how submitting data in a HTTP request works, especially in terms of the different HTTP methods (GET, POST etc) and specifying the names of the fields you submit (hint: it's not rocket science...the names of the parameters you send from the client must match what the server code is looking for....) – ADyson May 17 '23 at 12:46

0 Answers0