【Android+Struts2实现简单的前后台交互--Android网络编程】少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述Android+Struts2实现简单的前后台交互--Android网络编程相关的知识,希望能为你提供帮助。
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 21private TextView tv; 22private String mBaseUrl = "http://192.168.43.218:8080/OkHttpGetServer/"; 23@Override 24protected void onCreate(Bundle savedInstanceState) { 25super.onCreate(savedInstanceState); 26setContentView(R.layout.activity_main); 27} 28 29public void doGet(View view) { 30//拿到okHttpClient对象 31OkHttpClient okHttpClient = new OkHttpClient(); 32//构造Request 33Request request = new Request.Builder().get() 34.url(mBaseUrl+"login?username=Thanlon& password=123").build(); 35 //将Request封装成Call 36 //执行Call 37okHttpClient.newCall(request).enqueue(new Callback() { 38@Override 39public void onFailure(Call call, IOException e) { 40e.printStackTrace(); 41} 42 43@Override 44public void onResponse(Call call, Response response) throws IOException { 45String res = response.body().string(); 46showResultInfo(res); 47} 48}); 49} 50 51private void showResultInfo(final String resultInfo) { 52tv = (TextView) findViewById(R.id.tv); 53runOnUiThread(new Runnable() { 54@Override 55public void run() { 56tv.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 15private String username; 16private String password; 17 18public String login() throws IOException { 19 //System.out.println(username+ "," + password); 20HttpServletResponse response = ServletActionContext.getResponse(); 21response.setCharacterEncoding("utf-8"); //防止服务端发送到到客户端的数据出现中文乱码 22PrintWriter pw = response.getWriter(); 23pw.write("下面是服务器端返回的数据:\\n\\n"); 24pw.write("您提交的用户名是:"+username); 25pw.flush(); 26return null; 27} 28 29public String getUsername() { 30return username; 31} 32 33public void setUsername(String username) { 34this.username = username; 35} 36 37public String getPassword() { 38return password; 39} 40 41public void setPassword(String password) { 42this.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什么的还得多向大家学习……)
推荐阅读
- 通过 AppiumDriverLocalSerivce 无法启动 appium service 问题
- Android 动态注册JNI函数
- [yum]Another app is currently holding the yum lock
- Android APP开发内容图片不显示
- Android SQLite数据库升级,怎么做(事物更改)
- 配置文件——App.config文件读取和修改
- Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)
- 不安分的android开发者(小程序初尝试,前后台都自己做)
- Android进程机制