android与tomcat服务器交互实例

 

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"这是从服务器返回的消息。

最后 完整工程下载:

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值