First, there is already a project on the server side, which is based on SSH, which is already deployed on Tomcat and is running. I created a new package on the action layer for this project, which is primarily a Struts2 action, except that these actions do not return some view, Instead, the JSON is returned, and the XML configuration that defines the actions is as follows:
<package name= "Mobie" namespace= "/mobie" extends= "Json-default" > <action name= "Login" class= "loginaction" method= "Login" > <!-- Set return type to json --> <result name= "Success" type= " JSON "> < Param name= "Includeproperties" > user\.id,user\.email,user\.password,user\.gender,user\. tel,user\.nickname,user\.isactive </param> </result> &nbSp; </action> </package>
Then the main development of Android, on the Android side of my idea is like this: through the HTTP protocol to access the server-side action, the server returned to JSON, and then Android to accept JSON, by parsing JSON constructs an instance, It then displays feedback to the user on the activity. So, the main work on the Android side is to parse the JSON and display the data, and the work of parsing the JSON can be done with some packages to make it easier.
New Android Project: Import the jar package:
Interface file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns: Android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop = "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_ Margin " tools:context=" com.ddd3.ddd3.MainActivity " android:o rientation= "vertical" android:background= "@color/colorbackground" > <textview android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "JeanseLam?" android:textsize= "35DP" android:textcolor= "@color/colortext"/> <view android:layout_width= "Match_parent" android:layout_height= "1dip" android:background= "#696969" android:layout_margintop= "5DP" android:layout_marginbottom= "5DP"/> <textview android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "@string/ Mainurl "&NBSP;&NBSP;&NBSP;&NBsp; android:textsize= "20DP" android: Textcolor= "@color/colortext"/> <view android:layout_width= "Fill_parent" android:layout_height= "40DP" ></View> <TextView Android:id= "@+id/logininfo" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:textsize= "15DP" android: Textcolor= "@color/colorerror"/> <edittext android:id= "@+id/email" android:layout_width= "Fill_ Parent "&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; android:layout_height= "Wrap_content" Android:layout_marginbottom= "10DP" android:hint= "Type your email "/> <edittext android: Id= "@+id/password" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_marginbottom= "10DP" android: Password= "true" android:hint= "Enter password"/> <button android:id= "@+id/btn_login" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:background= "@drawable/button_ Selector " android:text=" Login android:textcolor= "@color/colorbtntext" android: Layout_marginbottom= "10DP"/> <button android:id= "@+id/btn_exit" android:layout_width= "Fill_ Parent " android:layout_height=" Wrap_content " android:text= "Exit" android: background= "@drawable/button_selector" android:textcolor= "@ Color/colorbtntext " android:layout_marginbottom=" 10DP "/> <textview android:id= "@+id/btn_register" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:text= "new user?" android:textcolor= "@color/colortext" android:autolink= "All"/></linearlayout>
Activity:
package com.ddd3.ddd3;import android.support.v7.app.appcompatactivity;import android.os.bundle; import android.util.log;import android.view.view;import android.widget.button;import android.widget.edittext;import android.widget.textview;import com.android.volley.defaultretrypolicy; import com.android.volley.request;import com.android.volley.requestqueue;import com.android.volley.response;import com.android.volley.volleyerror;import com.android.volley.toolbox.jsonobjectrequest;import com.android.volley.toolbox.stringrequest;import com.android.volley.toolbox.volley;import com.ddd3.entity.user;import com.ddd3.service.jsonservice; Public class mainactivity extends appcompatactivity { private EditText emailText; private EditText passwordText; private button btn_login; private button btn_exit; private textview info; private requestqueue requestqueue;//for network network requests, each activity requires only one requestqueue //private UserService userService; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); initview (); } private void initview () { emailText = (EditText) Findviewbyid (r.id.email); passwordText = (EditText) Findviewbyid (R.id.password); btn_login = (Button) Findviewbyid (r.id.btn_login); btn_exit = ( Button) Findviewbyid (r.id.btn_exit); info = (TextView) Findviewbyid (R.id.logininfo); btn_login.setonclicklistener (new buttonlistener ()); btn_exit.setonclicklistener (new Buttonlistener ()); requestqueue = Volley.newrequestqueue (this);//Instantiate request queue } class buttonlistener implements view.onclicklistener{ @ Override public void onclick (VIEW&NBSP;V) { if (V.getid () == btn_ Login.getid ()) { &Nbsp; string email = emailtext.gettext (). toString (); String Password = passwordtext.gettext (). toString (); if (Email.equals ("") | | password.equals ("")) { info.settext ("Please enter your email and password"); } else{ //Service End url string url = "http://192.168.56.1:8080/JeanseLam/mobie/login?email=" +email+ " &password= "+password; jsonobjectrequest jr = new jsonobjectrequest ( Request.method.get,url, null, new response.listener<org.json.jsonobject> () { @Override public void onresponse (Org.json.jsonobject jsonobject) { System.out.println ("------------------" + jsonobject.tostring ()); user user = (User) Jsonservice.jsonstring2object (jsonobject.tostring (), user.class); system.out.println (User.getemail () +user.getnickname ()); /** * Jump or display */ } }, new response.errorlistener () { @Override public void onerrorresponse (Volleyerror error) { system.out.println ("-------------------" +error.getmessage ()); } }) ; requestqueue.add (JR); /*** StringRequest stringRequest = new Stringrequest ("Http://192.168.56.1:8080/JeanseLam", new response.listeneR<string> () { @ override public void onresponse (String response) { system.out.println ("SUCCESS---------------->" +response); } }, new response.errorlistener () { @Override public void onerrorresponse (Volleyerror error) { system.out.println ("ERROR------------------>" + Error.getmessage ()); } }); stringrequest.setretrypolicy (New defaultretrypolicy (20 * 1000, 1, 1.0f)); requestqueue.add (stringrequest); */ } } Else if (V.getid () == btn_exit.getid ()) { android.os.process.killprocess (Android.os.Process.myPid ()); } } }}
It is important to note that because the local project is accessed, the URL address is: 192.168.56.1:8080 instead of localhost or other
Android Client Access Service-side tomcat