Android-use the struts2 server to interact with Android

Source: Internet
Author: User

I. server side:

First, build the struts2 environment and import necessary class libraries.

Web. xml file:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" <br/> xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> <filter-Name> struts2 </filter- name> <br/> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> struts2 </filter-Name> <br/> <URL-pattern>/* </url-pattern> <br/> </filter-mapping> <br/> </Web-app> <br/>
Struts. xml file:

<? XML version = "1.0" encoding = "GBK"?> <Br/> <! Doctype struts Public <br/> "-// Apache Software Foundation/DTD struts configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> <br/> <struts> <br/> <package name = "testjson" extends = "JSON-Default"> </P> <p> <action name = "getjson" class = "com. shao. action. jsonaction "method =" JSON "> <br/> <result type =" JSON "> </result> <br/> </Action> </P> <p> </package> <br/> </struts>
Action class:

Package COM. shao. action; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import Org. apache. struts2.interceptor. servletrequestaware; <br/> Import Org. apache. struts2.interceptor. servletresponseaware; </P> <p> Import COM. google. gson. gson; <br/> Import COM. opensymphony. xwork2.actionsupport; <br/> Import COM. shao. domain. music; </P> <p> public class jsonaction extends actionsupport implements servletrequestaware, <br/> servletresponseaware {</P> <p>/** <br/> */<br/> Private Static final long serialversionuid =-3604892179657815531l; <br/> private httpservletrequest request; <br/> private httpservletresponse response; <br/> private string format; <br/> Public String getformat () {<br/> return format; <br/>}</P> <p> Public void setformat (string format) {<br/> This. format = format; <br/>}</P> <p> @ override <br/> Public void setservletrequest (httpservletrequest request) {<br/> // todo auto-generated method stub <br/> This. request = request; <br/>}</P> <p> @ override <br/> Public void setservletresponse (httpservletresponse response) {<br/> // todo auto-generated method stub <br/> This. response = response; <br/>}</P> <p> Public void JSON () {<br/> List <music> List = new arraylist <music> (); <br/> // jsonarray = new jsonarray (); <br/> // jsonobject = new jsonobject (); <br/> gson = new gson (); <br/> music M1 = new music (); <br/> m1.setid (1); <br/> m1.setauthor ("You hongming "); <br/> m1.setname ("White lover"); <br/> m1.settime ("04:01"); <br/> list. add (M1); <br/> music m2 = new music (); <br/> m2.setid (2); <br/> m2.setauthor ("Eason Chan "); <br/> m2.setname (""); <br/> m2.settime ("04:44"); <br/> list. add (m2); <br/> music m3 = new music (); <br/> m3.setid (3); <br/> m3.setauthor ("Xie Yunfeng "); <br/> m3.setname ("yellow"); <br/> m3.settime (""); <br/> list. add (m3); <br/> JAVA. lang. reflect. type type = new COM. google. gson. reflect. typetoken <list <music> () {<br/> }. getType (); <br/> string beanlisttojson = gson. tojson (list, type); <br/> system. out. println ("gson -->" + beanlisttojson); <br/> try {<br/> response. setcharacterencoding ("GBK"); <br/> // response. setcontenttype ("text/XML; charset = UTF-8"); <br/> This. response. getwriter (). write (beanlisttojson); <br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>

This music entity class is also used by the android client.

Package COM. shao. domain; </P> <p> Public Class Music {</P> <p> private integer ID; <br/> private string name; <br/> private string time; <br/> private string author; <br/> Public integer GETID () {<br/> return ID; <br/>}< br/> Public void setid (integer ID) {<br/> This. id = ID; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String gettime () {<br/> return time; <br/>}< br/> Public void settime (string time) {<br/> This. time = time; <br/>}< br/> Public String getauthor () {<br/> return author; <br/>}< br/> Public void setauthor (string author) {<br/> This. author = author; <br/>}< br/>
Access http: // localhost: 8080/client/getjson. Action; Result:

Ii. Android client:

Activity Class:

Package COM. shao. main; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. list; <br/> Import Java. util. map; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. listview; <br/> Import android. widget. simpleadapter; </P> <p> public class jsonclientactivity extends activity {<br/>/** called when the activity is first created. */<br/> private button update; <br/> private listview; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Update = (button) findviewbyid (R. id. update); <br/> listview = (listview) findviewbyid (R. id. list); <br/> Update. setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> string urlstr = "http: // 10.0.2.2: 8080/client/getjson. action "; <br/> stringresult = gsonutil. getjson (urlstr); <br/> List <music> List = gsonutil. getlistfromjson (result); <br/> List <Map <string, Object> DATA = getadapterdata (list); </P> <p> simpleadapter adapter = new simpleadapter (jsonclientactivity. this, Data, R. layout. list, new string [] {"name", "author", "Time"}, new int [] {R. id. name, R. id. author, R. id. time}); <br/> listview. setadapter (adapter); <br/> // listview. <br/>}< br/>}); <br/>}< br/> private list <Map <string, Object> getadapterdata (list List) {<br/> List <Map <string, Object> DATA = new arraylist <Map <string, Object> (); <br/> for (INT I = 0; I <list. size (); I ++) {<br/> Map <string, Object> map = new hashmap <string, Object> (); <br/> music = (music) list. get (I); <br/> map. put ("name", music. getname (); <br/> map. put ("author", music. getauthor (); <br/> map. put ("time", music. gettime (); <br/> data. add (MAP); <br/>}< br/> return data; <br/>}< br/>}

Package COM. shao. main; </P> <p> Import java.net. uri; <br/> Import Java. util. list; </P> <p> Import Org. apache. HTTP. httpentity; <br/> Import Org. apache. HTTP. httpresponse; <br/> Import Org. apache. HTTP. client. httpclient; <br/> Import Org. apache. HTTP. client. methods. httppost; <br/> Import Org. apache. HTTP. impl. client. defaulthttpclient; <br/> Import Org. apache. HTTP. util. entityutils; </P> <p> Import COM. google. gson. gson; </P> <p> public class gsonutil {<br/> Public static string getjson (string URL) {</P> <p> httpclient client = new defaulthttpclient (); </P> <p> httppost request; <br/> try {<br/> request = new httppost (New uri (URL )); <br/> httpresponse response = client.exe cute (request); <br/> // determine whether the request is successful <br/> If (response. getstatusline (). getstatuscode () = 200) {// 200 indicates that the request is successful <br/> httpentity entity = response. getentity (); <Br/> If (entity! = NULL) {<br/> string beanlisttojson = entityutils. tostring (entity, "GBK"); <br/> return beanlisttojson; <br/>}</P> <p >} catch (exception E) {<br/> // todo: handle exception <br/>}< br/> return NULL; <br/>}< br/> Public static list <music> getlistfromjson (string JSON) {<br/> JAVA. lang. reflect. type type = new COM. google. gson. reflect. typetoken <list <music> () {<br/> }. getType (); <br/> gson = new gson (); <br/> List <music> List = gson. fromjson (JSON, type); <br/> return list; <br/>}< br/>
List. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/Name" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "name" <br/> <textview <br/> Android: Id = "@ + ID/author" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_below = "@ ID/Name" <br/> Android: paddingtop = "5px" <br/> Android: TEXT = "author" <br/> </textview> <br/> <textview <br/> Android: id = "@ + ID/Time" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_below = "@ ID/Name" <br/> Android: layout_aligntop = "@ ID/author" <br/> Android: layout_alignparentright = "true" <br/> Android: TEXT = "time"> <br/> </textview> <br/> </relativelayout> <br/>

Running result:

The main interaction is completed through goolge's gson.

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.