Я занимаюсь разработкой проекта для Android, и у меня возникла проблема: всплывающее сообщение не отображается в моем приложении.Когда я тестирую его на своем телефоне, сообщение о тосте на тосте становится пустым.
здесь есть Register.java
public class Register extends AppCompatActivity implements View.OnClickListener {
private EditText etUsername, etPassword, etPassword2, etEmail;
private Button bRegis;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
etPassword2= (EditText) findViewById(R.id.etPassword2);
etEmail = (EditText) findViewById(R.id.etEmail);
bRegis = (Button) findViewById(R.id.bRegis);
progressDialog = new ProgressDialog(this);
bRegis.setOnClickListener(this);
}
private void RegisterUser(){
final String Email = etEmail.getText().toString().trim();
final String Username = etUsername.getText().toString().trim();
final String Password = etPassword.getText().toString().trim();
progressDialog.setMessage("Registering User...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("Username", Username);
params.put("Email", Email);
params.put("Password", Password);
return params;
}
};
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
}
@Override
public void onClick(View view){
String Password = etPassword.getText().toString();
String Password2= etPassword2.getText().toString();
if(Password2.equals(Password) && view == bRegis) {
RegisterUser();
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error");
builder.setMessage("Password does not match");
builder.setPositiveButton("Ok", null);
AlertDialog dialog = builder.show();
}
}
}
, а вот файл register.php
<?php
require_once 'DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(
isset($_POST['Username']) and
isset($_POST['Email']) and
isset($_POST['Password']))
{
//operate the data further
$db = new DbOperations();
$result = $db->createUser( $_POST['Username'],
$_POST['Password'],
$_POST['Email']
);
if($result == 1){
$response["error"] = false;
$response["message"] = "User registered successfully";
}elseif($result == 2){
$response["error"] = true;
$response["message"] = "Some error occurred please try again";
}elseif($result == 0){
$response["error"] = true;
$response["message"] = "It seems you are already registered, please choose a different email and username";
}
}else{
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}else{
$response['error'] = true;
$response['message'] = "Invalid Request";
}
echo json_encode($response);
?>
вот DbOperations.php
<?php
class DbOperations{
private $con;
function __construct(){
require_once dirname(__FILE__).'/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
/*CRUD -> C -> CREATE */
public function createUser($Username, $Password, $Email){
if($this->isUserExist($Username,$Email)){
return 0;
}else{
$Password = md5($Password);
$stmt = $this->con->prepare("INSERT INTO `User` (`id`, `Username`, `Password`, `Email`) VALUES (NULL, ?, ?, ?);");
$stmt->bind_param("sss",$Username,$Password,$Email);
if($stmt->execute()){
return 1;
}else{
return 2;
}
}
}
public function UserLogin($Username, $pass){
$Password = md5($pass);
$stmt = $this->con->prepare("SELECT id FROM User WHERE Username = ? AND Password = ?");
$stmt->bind_param("ss",$Username,$Password);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
public function getUserByUsername($Username){
$stmt = $this->con->prepare("SELECT * FROM User WHERE Username = ?");
$stmt->bind_param("s",$Username);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
private function isUserExist($Username, $Email){
$stmt = $this->con->prepare("SELECT id FROM User WHERE Username = ? OR Email = ?");
$stmt->bind_param("ss", $Username, $Email);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
?>
вот изображение моей проблемы:
пустое сообщение на телефоне
Спасибозаранее извините за мой плохой английский