摘要:本文将带你了解Android开发之Android+Struts2实现简单的前后台交互,希望本文对大家学Android有所帮助。
1、下面测试截图,基本过程就是:点击按钮向服务器端发送请求,后台收到请求后给出返回的数据,前台只需要显示服务端数据就可以了。例子很简单能但引发很多思考。
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 = "//192.168.43.218:8080/OkHttpGetServer/";23 @Override24 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 //构造Request33 Request request = new Request.Builder().get()34 .url(mBaseUrl+"login?username=Thanlon&password=123").build();35 // 将Request封装成Call36 // 执行Call37 okHttpClient.newCall(request).enqueue(new Callback() {38 @Override39 public void onFailure(Call call, IOException e) {40 e.printStackTrace();41 }42 43 @Override44 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 @Override55 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="//schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//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 "//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>
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号