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.
<?php //post方式,loginName,loginPwd相当于是key $name = $_POST['loginName']; $pwd = $_POST['loginPwd']; if($name!="hello"){ die ("用户名错误!"); } if($pwd !="wode"){ die ("密码错误!"); } echo "登录成功!"; ?>
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout> <edittext></edittext> <edittext></edittext> <button></button> </android.support.constraint.constraintlayout>
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>
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!