我正在开发一个android项目,但遇到一个问题:Toast消息未显示在我的应用程序上。当我在手机上对其进行测试时,吐司消息在吐司上变为空白。
这是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;
}
}
?>
这是我的问题的图像:
在此先感谢您的英语不好
答案 0 :(得分:0)
这是我简单的Api.php文件。希望这会有所帮助:
servers
答案 1 :(得分:0)
在最终变量之后,还要定义以下变量:
final Activity currentActivity = this;
现在,在Toast中使用此变量代替getApplicationContext()
:
Toast.makeText(currentActivity, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
或
Toast.makeText(currentActivity, error.getMessage(), Toast.LENGTH_LONG).show();