Android and PHP implement simple login

不言
Release: 2023-04-03 07:04:01
Original
4146 people have browsed it

First effect:


Android and PHP implement simple login

Instructions:

1, use an Android phone or emulator
2, the server supports php
3, simple verification login operation
4, the network request uses the RxEasyHttp open source library, which is an encapsulation of okhttp3, retrofit, it is also possible to use okhttp3 directly
5, used for test login The url is always valid
6. If you need a server, you can use it with me. You can just send a few soft coins. There is no traffic limit or space limit.

Server-side php file

<?php     //post方式,loginName,loginPwd相当于是key
    $name = $_POST[&#39;loginName&#39;];
    $pwd = $_POST[&#39;loginPwd&#39;];
    if($name!="hello"){
        die ("用户名错误!");
    }
    
    if($pwd !="wode"){
        die ("密码错误!");
    }
    
    echo "登录成功!";
    
?>
Copy after login

Android-side XML layout file

<?xml  version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout>


    <edittext></edittext>

    <edittext></edittext>

    <button></button>



</android.support.constraint.constraintlayout>
Copy after login

Android-side java code implementation

public class MainActivity extends AppCompatActivity {

    String loginUrl = "http://soyoyo.esy.es/login.php";

    EditText etName;
    EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName  = findViewById(R.id.etName);
        etPwd = findViewById(R.id.etPwd);

    }

    // 点击button后执行
    public void doLogin(View view){
        login(etName.getText().toString(),etPwd.getText().toString());
    }

    private void login(String name,String pwd){
        EasyHttp.post(loginUrl)
                .baseUrl(url)
                .params("loginName",name)
                .params("loginPwd", pwd)
                .execute(new CallBack<string>() {

                    @Override
                    public void onStart() {
                        System.out.println("------onStart------");
                    }

                    @Override
                    public void onCompleted() {
                        System.out.println("------onCompleted------");
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(String s) {
                        System.out.println("------onSuccess------" + s);
                        Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
                    }
                });
    }

}</string>
Copy after login

The above is the entire content of this article, I hope it will be useful to Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

php How to implement random numbers and letters verification code code

php tips how to cleverly avoid Some bad code in PHP programs

The above is the detailed content of Android and PHP implement simple login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template