Previously wrote a Java web-side login verification, and finally returned a JSON string.
The string format is as follows:
{"Appmsg": "Account or password Error", "Appcode": 0, "_default_boolean_a": false}
Today, combine Android to write a simple login.
Note: Permissions to access the network in Androidmanifest.xml
When writing a specific path, be sure to take care not to use localhost, because it cannot be identified. Be sure to write a specific IP
The steps are as follows:
1, first do the page layout
2, in the activity to obtain the user name, password
3, the user name, password to attach to the requested address
4, new a sub-thread, in the child thread through the HttpURLConnection to request data
5. Determine if the connection is successful based on the status code returned by the connection
6, successful connection, the acquisition of response data, return InputStream stream
7. Returns the result of the string type to the UI thread through the message loop mechanism of the UI thread
8. Processing the results returned in a child thread in the UI thread
9, the result of the string is converted to Jsonobject object, to get the code result in the object, 0 for the login failure, 1 for the login success.
10. Popup toast, prompting the user for failed or successful login.
First, Layout page
The first is a page layout, as follows
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " xmlns:app= "Http://schemas.android.com/apk/res-auto" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:orientation= "Vertical" > <imageview android:id= "@+id/imageView" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Center_horizontal" app:srccompat= "@drawable/qq"/> < Edittext android:id= "@+id/et_qqnum" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:ems= "ten" android:hint= "Please enter QQ number" android:inputtype= "Textpersonname"/> <edittext android:id= "@+id/et_pwd" android:layout _width= "Match_parent" android:layout_height= "Wrap_content" android:ems= "Ten" android:hint= "Enter password" &NBSP;&NBSP;&NBSP;&NBsp; android:inputtype= "Textpassword" / > <checkbox android:id= "@+id/cb_ Rember " android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:hint= "Remember Password" /> < Button android:id= "@+id/bt_sub" android:layout_width= "Match_parent" android: layout_height= "wrap_content" android:onclick= "Login" android:text= "Submit"/></linearlayout>
2, Mainactivity
Package com.yuanlp.qqloginweb;import android.content.sharedpreferences;import android.os.bundle ;import android.os.handler;import android.os.message;import android.support.v7.app.appcompatactivity;import android.text.textutils;import android.view.view; import android.widget.button;import android.widget.checkbox;import android.widget.edittext; Import android.widget.toast;import org.json.jsonexception;import org.json.jsonobject;import java.io.bufferedreader;import java.io.inputstream;import java.io.inputstreamreader;import java.net.httpurlconnection;import java.net.url;public class mainactivity extends appcompatactivity { private static final int load_ success =1 ; private static final int load_error =2 ; private edittext mqqnum; &nBsp; private edittext mqqpwd; private checkbox mcb_ Rember; /** * a handler in the main thread to get message in a child thread * */ handler handler=new handler () { @Override public void handlemessage (message msg) { switch (msg.what) { case LOAD_SUCCESS: boolean flag=checklogin (Msg.obj.toString ()); if (flag) {&NBSP;&Nbsp; toast.maketext (Mainactivity.this, "Login Successful", Toast.length_short). Show (); } else { toast.maketext (mainactivity.this, "Login Failed", Toast.LENGTH_SHORT). Show (); } sub.setenabled (True); } } }; private button&Nbsp;sub; private string mqq; private string mPwd; @Override protected void oncreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_main); mQqNum= (EditText) findviewbyid (r.id.et_qqnum); mQqPwd = (EditText) findviewbyid (r.id.et_pwd); mCb_rember = (CheckBox) findviewbyid (R.id.cb_rember); sub = (Button) findviewbyid (r.id.bt_sub); } public void login (View view) { //toast.maketext(This, "click Submit", Toast.length_short). Show (); mqq = Mqqnum.gettext (). toString (). Trim (); mpwd = Mqqpwd.gettext (). toString (). Trim (); //mcb_rember.gettext (). ToString (). Trim (); if (Textutils.isempty (mqq) | | Textutils.isempty (mpwd)) { Toast.maketext (This, "QQ number or password is empty", Toast.length_short). Show (); return; }else { //Selected Save Password if (mcb_rember.ischecked ()) { } } //here the Set button can not point, should be the point, always send the request, will cause the request data sub.setenabled (False); /** * Click the button event to open a child thread in the main thread for network request * (because in 4 only the main thread does not support the network request, it is generally recommended to turn on the sub-thread for network requests and other time-consuming operations). */ //Request Network new thread () { @Override public void run () { try&nBsp { thread.sleep (; ) string path= "Http://192.168.1.111:10010/aos/pdaLogin.jhtml? Username= "+ mqq +" &password= "+ mpwd; url url = new url (path); //Open httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openconnection (); &nbsP; conn.setrequestmethod ("GET"); //set get mode get Data conn.setconnecttimeout (; ) //Setting the connection timeout time 5 seconds int code = Conn.getresponsecode (); // Gets the response state, 200 indicates a successful resource acquisition, 404 indicates that the resource does not exist if (code==200) { inputstream is= Conn.getinputstream (); bufferedreader br=new bufferedreader (New inputstreamreader (IS)); stringbuffer sb=new stringbuffer (); string len=null; while ((Len=br.readline ())!=null) { sb.append (len); } String Result=sb.tostring (); /** * Child threads send messages to the main thread and bring the resulting results to the main thread, allowing the main thread to update the UI. */ message msg= message.obtain ( ); msg.what=load_success; msg.obj=result; handler.sendmessage (msg); } } catch (Exception e) { e.printstacktrace (); message msg=message.obtain (); msg.what=load_error; Handler.sendmessage (msg); } } }.start (); } private boolean Checklogin (String result) { boolean flag=false; JSONObject jsonObject=null; try { jsonobject =new jsonobject (result); string code=jsonobject.gET ("Appcode"). ToString (); if ("0". Equals (code)) { Flag=false; }else if ("1". Equals ( Code) { flag= true; } } catch (jsonexception e) { e.printstacktrace (); } return flag; }}
This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1941109
Android Access Web-side and parse JSON, impersonate user Login