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