First on the effect:
Description
1, using an Android phone, or simulator
2, Server support PHP
3, Simple Authentication login operation
4, the network request uses the Rxeasyhttp Open Source Library, is the encapsulation to the Okhttp3,retrofit, the direct use OKHTTP3 is also possible
5, the URL used to test login has been valid
6, need the server, you can share with me, casually sent a few soft sister coins are OK, unlimited traffic, unlimited space.
Server-side PHP files
<?php //post Way, loginname,loginpwd equivalent is key $name = $_post[' LoginName ']; $pwd = $_post[' loginpwd '); if ($name! = "Hello") {die ("username wrong!") "); } if ($pwd! = "Wode") {die ("Password wrong!"); } echo "Login Successful! "; ? >
Android-side XML layout file
<?xml version= "1.0" encoding= "Utf-8"? ><android.support.constraint.constraintlayout xmlns:android= "http:/ /schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools="/HTTP/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ng= "4DP" tools:context= ". Mainactivity "> <edittext android:id=" @+id/etname "android:layout_width=" Match_parent "Androi d:layout_height= "Wrap_content" android:layout_marginend= "8DP" android:layout_marginleft= "8DP" Android : layout_marginright= "8DP" android:layout_marginstart= "8DP" android:layout_margintop= "8DP" Android:hin t= "Please enter user name" app:layout_constraintend_toendof= "parent" app:layout_constraintstart_tostartof= "Parent" AP p:layout_constrainttop_totopof= "parent"/> <edittext android:id= "@+id/etpwd" android:layout_width= " Match_parenT "android:layout_height=" wrap_content "android:layout_marginend=" 8DP "android:layout_marginleft=" 8DP "Android:layout_marginright=" 8DP "android:layout_marginstart=" 8DP "android:layout_margintop=" 8DP " android:hint= "Please enter password" app:layout_constraintend_toendof= "parent" app:layout_constraintstart_tostartof= "PA Rent "app:layout_constrainttop_tobottomof=" @+id/etname "/> <button android:layout_width=" Wrap_conte NT "android:layout_height=" Wrap_content "android:layout_marginend=" 8DP "android:layout_marginleft=" 8d P "android:layout_marginright=" 8DP "android:layout_marginstart=" 8DP "android:layout_margintop=" 20DP " android:onclick= "Dologin" android:text= "Login" app:layout_constraintend_toendof= "parent" App:layo ut_constraintstart_tostartof= "Parent" app:layout_constrainttop_tobottomof= "@+id/etpwd"/></ Android.support.constraint.ConstraintLayouT>
Android
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); }//Click button to execute 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.printstac Ktrace (); } @Override public void onsuccess (String s) {System.out.prin TLN ("------onsuccess------" + s); Toast.maketext (Mainactivity.this,s,toast.length_short). Show (); } }); }}
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!