1. 程式人生 > >Android+Struts2實現簡單的前後臺交互--Android網絡編程

Android+Struts2實現簡單的前後臺交互--Android網絡編程

請求 PE tools activity conf gets AD build -a

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實現簡單的前後臺交互--Android網絡編程