我在android studio上尝试用户注册和登录活动。一切都很好。我的PHP脚本在输入数据或通过网页登录时响应。但是在Android应用程序中做同样的事情。我没有收到服务器响应字符串。但是,数据已成功添加到WAMP localhost服务器上的数据库中。
当我点击按钮时,我总是收到此错误;添加了数据,但没有响应,登录也一样。我的代码是:
public class RegisterActivity extends AppCompatActivity {
EditText etname,etusername,etemail,etpassword,etcnpassword;
Button bregister;
TextView signin;
String name,username,email,password,cnpassword;
AlertDialog.Builder builder;
String reg_url="http://192.168.1.4/Register1.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etname = (EditText) findViewById(R.id.name);
etusername=(EditText) findViewById(R.id.username);
etemail = (EditText) findViewById(R.id.email);
etpassword = (EditText) findViewById(R.id.password);
etcnpassword = (EditText) findViewById(R.id.cn_password);
builder = new AlertDialog.Builder(RegisterActivity.this);
bregister = (Button) findViewById(R.id.register_button);
signin = (TextView) findViewById(R.id.tv_signin);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent login = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(login);
}
});
bregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = etname.getText().toString();
username=etusername.getText().toString();
email = etemail.getText().toString();
password = etpassword.getText().toString();
cnpassword = etcnpassword.getText().toString();
if (name.equals("") || username.equals("")||email.equals("") || password.equals("") || cnpassword.equals("")) {
builder.setTitle("Error");
builder.setMessage("Please fill all the fields");
displayAlert("input_error");
}
else
{
if(!(password.equals(cnpassword))){
builder.setTitle("Error");
builder.setMessage("Passwords not matching.");
displayAlert("input_error");
}
else if(password.equals(cnpassword)){
StringRequest stringrequest=new StringRequest(Request.Method.POST, reg_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String code=jsonObject.getString("code");
String message=jsonObject.getString("message");
builder.setTitle("Server Response");
builder.setMessage(message);
displayAlert(code);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params=new HashMap<String, String>();
params.put("name",name);
params.put("username",username);
params.put("email",email);
params.put("password",password);
return params;
}
};
mysingleton.getInstance(RegisterActivity.this).addtorequestqueue(stringrequest);
}
else
;
}
}
});
}
public void displayAlert(final String code){
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (code.equals("input_error")) {
etpassword.setText("");
etcnpassword.setText("");
} else if (code.equals("Registration success")) {
finish();
/* Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(intent);*/
} else if (code.equals("Registration failed")) {
etname.setText("");
etusername.setText("");
etemail.setText("");
etpassword.setText("");
etcnpassword.setText("");
}
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
php代码是 -
<?php
require "init.php";
$name=$_POST["name"];
$username=$_POST["username"];
$email=$_POST["email"];
$password=$_POST["password"];
$sql="select * from login where email like '".$email."'; ";
$result=mysqli_query($con,$sql);
$response=array();
if(mysqli_num_rows($result)>0)
{
$code="Registration failed";
$message="User already exist";
array_push($response,array($code,$message));
echo json_encode($response);
}
else
{
$sql="insert into login(name,username,email,password) values('".$name."','".$username."','".$email."','".$password."');";
$result=mysqli_query($con,$sql);
$code="Registration success";
$message="Thank you for registering with us. Now you can log in.";
array_push($response,array($code,$message));
echo json_encode($response);
}
mysqli_close($con);
?>
错误是什么以及如何解决?