android与tomcat服务器交互实例
一、教程目的。
本教程的目的在于教会怎么完成android与tomcat服务器的数据交互。
二、开发环境。
Myeclipse8.5+jdk1.6+android2.3
三、开发步骤。
(1)新建一个web项目,并添加struts2支持。
new->webproject
1在项目名称右键,Myeclipse->Add Struts Capabilities
struts specification 选择struts2.1
然后选择完成
2新建一个action类
package action; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AndroidAction extends ActionSupport { HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); @Override public String execute() throws Exception { // TODO Auto-generated method stub return super .execute(); } public void test() { String str = request.getParameter( "test" ); System.out.println(str); writeOut( "hello android" ); } /** * 返回值 * @param jsonStr */ public void writeOut(String jsonStr) { response.setContentType( "html/txt" ); response.setCharacterEncoding( "utf-8" ); response.setHeader( "Pragma" , "no-cache" ); response.setHeader( "Cache-Control" , "no-cache, must-revalidate" ); response.setHeader( "Pragma" , "no-cache" ); try { response.getWriter().write(jsonStr); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { e.printStackTrace(); } } } |
3.修改struts.xml
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" > <struts> < package name= "androidaction" extends = "json-default" namespace= "/androidaction" > <action name= "test" class = "action.AndroidAction" method= "test" > <result type= "json" name= "success" > <param name= "contentType" >text/html</param> </result> <result type= "json" name= "error" > <param name= "contentType" >text/html</param> </result> </action> </ package > </struts> |
4.发布web项目 AndroidWebserver上右键 run as Myeclipse server application
(2)新建一个android项目。
记得要加入commons-httpclient-3.1.jar依赖包
新建一个链接工具类ConnUtil.java代码如下
public class HelloApacheServerActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final ConnUtil cu = new ConnUtil(); Button btn = (Button)findViewById(R.id.sendMessage); btn.setOnClickListener( new OnClickListener(){ @Override public void onClick(View arg0) { String s = cu.sayHello(); Toast.makeText(HelloApacheServerActivity. this ,s,Toast.LENGTH_SHORT).show(); } }); } } |
对了,android项目要记得添加网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
最后验证一下
按下"say hello"按钮 在myeclipse 的console下可以看到"hello server" 这是从android端发来的消息
同时android会弹出一个Toast "hello android"这是从服务器返回的消息。
最后 完整工程下载:
https://files.cnblogs.com/feifei1010/Desktop.zip
欢迎热爱安卓开发者加入群共同进步。南京群 220818530,武汉群121592153,,杭州群253603803,厦门群253604146,湖南群217494504,大连群253672904
青岛群 257925319
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!