Interaction instance between Android and Tomcat server

Source: Internet
Author: User

1. Tutorial purpose.

This tutorial aims to teach you how to complete data interaction between Android and tomcat servers.
2. development environment.
Myeclipse8.5 + jdk1.6 + android2.3
3. Development steps.
(1) create a web project and add struts2 support.
New-> webproject
1. Right-click the project name, and choose myeclipse>Add struts capabilities 

Struts specification select struts2.1
Then select complete

2. Create an action class

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.exe cute ();} public void test () {string STR = request. getparameter ("test"); system. out. println (STR); writeout ("Hello android");}/*** return value * @ 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. Modify 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. Publish the WEB Project androidwebserver right-click Run as myeclipse server application

(2) create an android project.
Remember to add the commons-httpclient-3.1.jar dependency package
Create a link tool class connutil. JavaCodeAs follows:

 
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 ();}});}}

By the way, remember to add network access permissions for the android project.
<Uses-Permission Android: Name = "android. Permission. Internet"/>

Verify
Press the "Say hello" button and you can see "Hello server" in the console of myeclipse. This is a message sent from Android.
At the same time, Android will pop up a toast "Hello android" message, which is returned from the server.

Download the complete project:

Http://files.cnblogs.com/feifei1010/Desktop.zip

Welcome Android Developers to join the group for common progress.Nanjing group 220818530,Wuhan group 121592153, HangzhouState group 253603803,Xiamen group 253604146,Hunan group 217494504,Dalian group 253672904
Qingdao group 257925319

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.