1, the following test, the basic process is: Click the button to send a request to the server, the background received the request after the return of the data, the front desk only need to display the data on the service side can be. The example is very simple can but triggered a lot of thinking, Bo Master learned assorted, this is to think of hybrid Android? ...... In fact, want to know more about other things, after all, Bo Master is still in college Ah! There's nothing wrong with learning more ...
2. Client (Android) key codeMainactivity.java:
1 PackageThonlon.example.cn.sendgetdemo;2 3 ImportAndroid.inputmethodservice.KeyboardView;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.Button;8 ImportAndroid.widget.ScrollView;9 ImportAndroid.widget.TextView;Ten One Importjava.io.IOException; A - ImportOKHTTP3. Call; - ImportOKHTTP3. Callback; the ImportOKHTTP3. okhttpclient; - ImportOKHTTP3. Request; - ImportOKHTTP3. Response; - + Public classMainactivityextendsappcompatactivity { - + PrivateTextView TV; A PrivateString Mbaseurl = "http://192.168.43.218:8080/OkHttpGetServer/"; at @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - } - in Public voiddoget (view view) { - //get the Okhttpclient object . toOkhttpclient okhttpclient =Newokhttpclient (); + //Construct Request -Request Request =NewRequest.builder (). Get () the. URL (mbaseurl+ "login?username=thanlon&password=123"). Build (); * //encapsulate the request as a call $ //Execute CallPanax NotoginsengOkhttpclient.newcall (Request). Enqueue (NewCallback () { - @Override the Public voidonfailure (call call, IOException e) { + e.printstacktrace (); A } the + @Override - Public voidOnresponse (call call, Response Response)throwsIOException { $String res =response.body (). String (); $ Showresultinfo (res); - } - }); the } - Wuyi Private voidShowresultinfo (FinalString Resultinfo) { theTV =(TextView) Findviewbyid (r.id.tv); -Runonuithread (NewRunnable () { Wu @Override - Public voidrun () { About Tv.settext (resultinfo); $ } - }); - } -}
Activity_main.xml: (layout also sent, make a reference)
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.constraint.ConstraintLayoutxmlns: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"Tools:context= "Thonlon.example.cn.sendgetdemo.MainActivity"> <ButtonAndroid:id= "@+id/btn_send"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:onclick= "Doget"Android:text= "Send request to server side (here with GET request as example)"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constraintvertical_bias= "0.0" /> <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"App:layout_constrainthorizontal_bias= "0.0"App:layout_constraintvertical_bias= "0.097" /></Android.support.constraint.ConstraintLayout>
3. Server-side main codeUserloginaction.java: (Servlet can also, here with the help of the STRUT2 framework)
1 Packagecom.okhttp;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.security.auth.message.callback.PrivateKeyCallback.Request;7 ImportJavax.servlet.http.HttpServletResponse;8 9 ImportOrg.apache.struts2.ServletActionContext;Ten One ImportCom.opensymphony.xwork2.ActionSupport; A - Public classUserloginactionextendsActionsupport { - the PrivateString username; - PrivateString password; - - PublicString Login ()throwsIOException { + //System.out.println (username+ "," + password); -HttpServletResponse response =servletactioncontext.getresponse (); +Response.setcharacterencoding ("Utf-8");//prevents data sent to clients by the server from appearing garbled in Chinese APrintWriter PW =Response.getwriter (); atPw.write ("The following is the data returned by the server side: \ n"); -Pw.write ("The user name you submitted is:" +username); - Pw.flush (); - return NULL; - } - in PublicString GetUserName () { - returnusername; to } + - Public voidSetusername (String username) { the This. Username =username; * } $ Panax Notoginseng PublicString GetPassword () { - returnpassword; the } + A Public voidSetPassword (String password) { the This. Password =password; + } -}Struts.xml:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Struts public3 "-//apache software foundation//dtd Struts Configuration 2.0//en"4 "Http://struts.apache.org/dtds/struts-2.0.dtd">5 <Struts>6 < Packagename= "Default"namespace="/"extends= "Struts-default">7 <Actionname= "Login"class= "Com.okhttp.UserLoginAction"Method= "Login">8 </Action>9 </ Package>Ten </Struts>
Attached: personal website www.nxl123.cn (background using Python flask framework, January 1, 2019 will be upgraded and officially enabled. Hey, I'm still a student dog! Website do not do a lot of advice and suggestions, don't scold me just fine! Hey...... After SEO what also have to learn from you ...)
ANDROID+STRUTS2 enables simple front-to-back interaction--android Network programming