报名(SignUp)REST服务在从Android应用程序发起的REST调用时不起作用。

huangapple go评论75阅读模式
英文:

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 = &quot;http://192.168.1.101/login.php&quot;;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener&lt;String&gt;() {
@Override
public void onResponse(String response) {
if (response.equals(&quot;not found&quot;)) {
Toast.makeText(getContext(), &quot;پست الکترونیک یا رمز عبور اشتباه است&quot;, Toast.LENGTH_SHORT).show();
} else {
onSignupClicked.onClicked(response);
dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(&quot;LOG&quot;, &quot;onErrorResponse: &quot; + error.toString());
}
}) {
@Override
protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
params.put(&quot;email&quot;, myEmail);
params.put(&quot;pass&quot;, 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 = &quot;http://192.168.1.101/signup.php&quot;;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener&lt;String&gt;() {
@Override
public void onResponse(String response) {
Log.i(&quot;LOG&quot;, &quot;onResponse: &quot;+response);
onSignupClicked.onClicked(response);
dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(&quot;LOG&quot;, &quot;onErrorResponse: &quot; + error.toString());
}
}) {
@Override
protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
params.put(&quot;email&quot;, email);
params.put(&quot;pass&quot;, 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 -->

&lt;?php
include &quot;connect.php&quot;;
$email = $_POST[&quot;email&quot;];
$pass = $_POST[&quot;pass&quot;];
$query = &quot;INSERT INTO user(email,pass)VALUES (:email,:pass)&quot;;
$res=$connect-&gt;prepare($query);
$res-&gt;bindParam(&quot;:email&quot;,$email);
$res-&gt;bindParam(&quot;:pass&quot;,$pass);
$res-&gt;execute();
if($res){
echo $email;
}else{
echo &quot;error&quot;;
}

<!-- 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

&lt;?php
$connection = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;password&#39;,&#39;user&#39;) or die(&#39;connecition err&#39;);
$email = $_POST[&#39;email&#39;];
$pass = $_POST[&#39;pass&#39;];
$sql = &quot;INSERT INTO `user` (`email`,`pass`) VALUES (&#39;$email&#39;,&#39;$pass&#39;)&quot;;
$query = mysqli_query($connection,$sql);
if($query){
echo $email;
}else{
echo &quot;no&quot;;
}
?&gt;

huangapple
  • 本文由 发表于 2020年5月29日 18:41:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/62084081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定