1

hi guys im trying to implement Login\Register in android using Volley and im using JSONObject and Repose.listener, and when im outside the listener all is good ad show here

but im "inside" the listener the password field is "missing" as shown here

LoginActivity

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

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

Login.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);
?>

this cause reponse: {"success":false} always

P.S the register works the same way (more or less) and it's working fine

Community
  • 1
  • 1
styx
  • 1,852
  • 1
  • 11
  • 22
  • 1
    `mysqli_stmt_bind_result($statement, $username, $password);` you're trying to bind 3 variables but only selecting 2 in the SELECT; that alone is failing and using `mysqli_error($con)` on the query would have thrown you something about it. – Funk Forty Niner Nov 18 '16 at 15:45
  • 1
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Nov 18 '16 at 15:46
  • @Fred-ii- according to PHP manual http://php.net/manual/en/mysqli-stmt.bind-result.php you can do it and also i aint getting no error – styx Nov 19 '16 at 10:04
  • @JayBlanchard i know that, but first the basic have to work, then you build on top of it – styx Nov 19 '16 at 10:05
  • @styx Fair enough. If you're not getting errors from php/mysql, then it's an Android problem and I won't be able to help you with this. – Funk Forty Niner Nov 19 '16 at 14:38

1 Answers1

0

I see you call a response, before even calling the Login. Where is it coming from? You seem to call the response (Response.Listener) before even sending the request (LoginRequest) in the bLogin.setOnClickListener. Please try to reverse them.

Maxime Claude
  • 965
  • 12
  • 27
  • so how can you explain that it "sees" the username but not the password?, also i did the same thing on registration and it worked fine – styx Nov 18 '16 at 13:44
  • please explain to me this line in your PHP file: mysqli_stmt_bind_param($statement, "ss", $username, $password); Try 'ss' instead of "ss" . Not sure if it will solve anything but worth giving a try. – Maxime Claude Nov 18 '16 at 14:08
  • *"Try 'ss' instead of "ss" "* - @MaximeClaude Doesn't make a difference; both type of quotes are acceptable. – Funk Forty Niner Nov 18 '16 at 15:45