Android+Struts2实现简单的前后台交互

1、下面测试截图,基本过程就是:点击按钮向服务器端发送请求,后台收到请求后给出返回的数据,前台只需要显示服务端数据就可以了。例子很简单能但引发很多思考,博主学了杂七杂八的,这又在来想杂学Android了?……其实是想多了解一下其他东西,毕竟博主还在上大学呀!多学点没什么不好……

2、客户端(Android)关键代码

MainActivity.java:

 1 package thonlon.example.cn.sendgetdemo;
 2 
 3 import android.inputmethodservice.KeyboardView;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.ScrollView;
 9 import android.widget.TextView;
10 
11 import java.io.IOException;
12 
13 import okhttp3.Call;
14 import okhttp3.Callback;
15 import okhttp3.OkHttpClient;
16 import okhttp3.Request;
17 import okhttp3.Response;
18 
19 public class MainActivity extends AppCompatActivity {
20 
21     private TextView tv;
22     private String mBaseUrl = "http://192.168.43.218:8080/OkHttpGetServer/";
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27     }
28 
29     public void doGet(View view) {
30         //拿到okHttpClient对象
31         OkHttpClient okHttpClient = new OkHttpClient();
32         //构造Request
33         Request request = new Request.Builder().get()
34                 .url(mBaseUrl+"login?username=Thanlon&password=123").build();
35 //        将Request封装成Call
36 //        执行Call
37         okHttpClient.newCall(request).enqueue(new Callback() {
38             @Override
39             public void onFailure(Call call, IOException e) {
40                 e.printStackTrace();
41             }
42 
43             @Override
44             public void onResponse(Call call, Response response) throws IOException {
45                 String res = response.body().string();
46                 showResultInfo(res);
47             }
48         });
49     }
50 
51     private void showResultInfo(final String resultInfo) {
52         tv = (TextView) findViewById(R.id.tv);
53         runOnUiThread(new Runnable() {
54             @Override
55             public void run() {
56                 tv.setText(resultInfo);
57             }
58         });
59     }
60 }

 activity_main.xml:(布局也发了,做个参考)

<?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"
    tools:context="thonlon.example.cn.sendgetdemo.MainActivity">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:onClick="doGet"
        android:text="向服务器端发送请求(这里以Get请求作为示例)"
        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" />

    <TextView
        android: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、服务器端主要代码

UserLoginAction.java:(Servlet自然也可以,这里借助了strut2框架)

 1 package com.okhttp;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.security.auth.message.callback.PrivateKeyCallback.Request;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionSupport;
12 
13 public class UserLoginAction extends ActionSupport {
14 
15     private String username;
16     private String password;
17 
18     public String login() throws IOException {
19 //        System.out.println(username+ "," + password);
20         HttpServletResponse response = ServletActionContext.getResponse();
21         response.setCharacterEncoding("utf-8");  //防止服务端发送到到客户端的数据出现中文乱码
22         PrintWriter pw = response.getWriter();
23         pw.write("下面是服务器端返回的数据:\n\n");
24         pw.write("您提交的用户名是:"+username);
25         pw.flush();
26         return null;
27     }
28 
29     public String getUsername() {
30         return username;
31     }
32 
33     public void setUsername(String username) {
34         this.username = username;
35     }
36 
37     public String getPassword() {
38         return password;
39     }
40 
41     public void setPassword(String password) {
42         this.password = password;
43     }
44 }

 struts.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 <struts>
 6     <package name="default" namespace="/" extends="struts-default">
 7         <action name="login" class="com.okhttp.UserLoginAction" method="login">
 8         </action>
 9     </package>
10 </struts>

附:个人网站www.nxl123.cn(后台采用Python Flask框架搭建,2019年1月1日将升级完成并正式启用。哎,本人还是学生狗呢!网站做的不好多多提点意见和建议吧,别骂我就好!嘿嘿……以后SEO什么的还得多向大家学习……)

Android+Struts2实现简单的前后台交互

全文结束