英文:
SignUp rest service is not working when rest call is made from Android Application
问题
public class LoginDialog extends DialogFragment {
EditText edtEmail, edtPass;
Button btnSignup, btnLogin;
OnSignupClicked onSignupClicked;
View view;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
view = LayoutInflater.from(getContext()).inflate(R.layout.login_dialog, null);
setupViews();
builder.setView(view);
return builder.create();
}
private void setupViews() {
// ... (Button and EditText initialization)
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = edtEmail.getText().toString();
String pass = edtPass.getText().toString();
userSignup(email, pass);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login(edtEmail.getText().toString(), edtPass.getText().toString());
}
});
}
private void login(final String myEmail, final String pass) {
// ... (Login request implementation)
}
public void setOnSignupClicked(OnSignupClicked onSignupClicked) {
this.onSignupClicked = onSignupClicked;
}
private void userSignup(final String email, final String pass) {
String url = "http://192.168.1.101/signup.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("LOG", "onResponse: " + response);
onSignupClicked.onClicked(response);
dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public interface OnSignupClicked {
void onClicked(String email);
}
}
<?php
include "connect.php";
$email = $_POST["email"];
$pass = $_POST["pass"];
$query = "INSERT INTO user(email,pass) VALUES (:email,:pass)";
$res = $connect->prepare($query);
$res->bindParam(":email", $email);
$res->bindParam(":pass", $pass);
$res->execute();
if ($res) {
echo $email;
} else {
echo "error";
}
?>
Please note that the provided code snippets contain both Java (Android) and PHP code. It seems that the PHP code is responsible for handling the signup process and adding user data to the database. The Java code, on the other hand, is responsible for creating a login dialog and making network requests using the Volley library.
If you encounter issues with signup not inserting data into the database, make sure to check your server configuration, database connection, and error logs. Also, ensure that the URLs in your Java code match the actual server address where your PHP scripts are hosted.
英文:
I create project in android studio and use Volley for signup and login
I can login in my android app with email and password that i insert in database with phpMyadmin But signup in android doesn't insert email and password to database!
this is my LoginDialog.java code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
public class LoginDialog extends DialogFragment {
EditText edtEmail, edtPass;
Button btnSignup, btnLogin;
OnSignupClicked onSignupClicked;
View view;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
view = LayoutInflater.from(getContext()).inflate(R.layout.login_dialog, null);
setupViews();
builder.setView(view);
return builder.create();
}
private void setupViews() {
btnSignup = (Button) view.findViewById(R.id.btn_loginDialog_signup);
edtEmail = (EditText) view.findViewById(R.id.edt_loginDialog_email);
edtPass = (EditText) view.findViewById(R.id.edt_loginDialog_pass);
btnLogin = (Button) view.findViewById(R.id.btn_loginDialog_login);
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = edtEmail.getText().toString();
String pass = edtPass.getText().toString();
userSignup(email, pass);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login(edtEmail.getText().toString(), edtPass.getText().toString());
}
});
}
private void login(final String myEmail, final String pass) {
String url = "http://192.168.1.101/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("not found")) {
Toast.makeText(getContext(), "پست الکترونیک یا رمز عبور اشتباه است", Toast.LENGTH_SHORT).show();
} else {
onSignupClicked.onClicked(response);
dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", myEmail);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public void setOnSignupClicked(OnSignupClicked onSignupClicked) {
this.onSignupClicked = onSignupClicked;
}
private void userSignup(final String email, final String pass) {
String url = "http://192.168.1.101/signup.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("LOG", "onResponse: "+response);
onSignupClicked.onClicked(response);
dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public interface OnSignupClicked {
void onClicked(String email);
}
}
<!-- end snippet -->
And this my signup.php code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?php
include "connect.php";
$email = $_POST["email"];
$pass = $_POST["pass"];
$query = "INSERT INTO user(email,pass)VALUES (:email,:pass)";
$res=$connect->prepare($query);
$res->bindParam(":email",$email);
$res->bindParam(":pass",$pass);
$res->execute();
if($res){
echo $email;
}else{
echo "error";
}
<!-- end snippet -->
Please guide me how can i fix signup to connect with database :X
答案1
得分: 0
您的服务器端代码存在错误。我已经修复了它。
<?php
$connection = mysqli_connect('localhost','root','password','user') or die('连接错误');
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "INSERT INTO `user` (`email`,`pass`) VALUES ('$email','$pass')";
$query = mysqli_query($connection,$sql);
if($query){
echo $email;
}else{
echo "no";
}
?>
英文:
Your server side code had bugs. I fixed it
<?php
$connection = mysqli_connect('localhost','root','password','user') or die('connecition err');
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "INSERT INTO `user` (`email`,`pass`) VALUES ('$email','$pass')";
$query = mysqli_query($connection,$sql);
if($query){
echo $email;
}else{
echo "no";
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论