Libs
Web. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Define the core controller of struts2: filterdispatcher --> <filter> <! -- Define the name of the core filter --> <filter-Name> struts2 </filter-Name> <! -- Define the implementation class of filter --> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>
Struts. xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.i18n.encoding" value="utf-8"></constant><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><package name="default" extends="json-default" namespace="/"><action name="getjson" class="com.zte.android.LoginAction"method="login"><result type="json"></result></action></package></struts>
Student. Java
package com.zte.android;public class Student{private String name;private String age;private String school;private String phone;public String getName(){return name;}public void setName(String name){this.name = name;}public String getAge(){return age;}public void setAge(String age){this.age = age;}public String getSchool(){return school;}public void setSchool(String school){this.school = school;}public String getPhone(){return phone;}public void setPhone(String phone){this.phone = phone;}}
Login. Action:
Package COM. ZTE. android; import Java. io. ioexception; import Java. io. unsupportedencodingexception; import Java. util. arraylist; import Java. util. list; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts2.interceptor. servletrequestaware; import Org. apache. struts2.interceptor. servletresponseaware; import COM. google. gson. gson; import COM. opensympho NY. xwork2.actionsupport; public class loginaction extends actionsupport implements attributes, attributes {/*****/Private Static final long serialversionuid = 1l; httpservletrequest request; httpservletresponse response; private string username; private string password; public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} publi C string GetUserName () {return username;} public void setusername (string username) {This. username = username;} public void setservletrequest (httpservletrequest request) {This. request = request;} public void setservletresponse (httpservletresponse response) {This. response = response;}/*** simulate User Login business */Public void login () {returnuserinfo ();} private void returnuserinfo () {This. response. setcontenttype ("text/JSO N; charset = UTF-8 "); this. response. setcharacterencoding ("UTF-8"); string JSON; If (username = NULL | Password = NULL) {JSON = "request parameter error"; flushdata (JSON); return ;} if (username. equals ("123") & password. equals ("123") {list <student> List = new arraylist <student> (); gson = new gson (); For (INT I = 0; I <10; I ++) {student ST = new student (); ST. setage ("10" + I); ST. setname ("CSH" + I); ST. setphone ("1333007" + I); ST. setschool ("ABC" + I); list. add (ST);} JSON = gson. tojson (list);} else {JSON = "illegal login information! ";}Flushdata (JSON);} private void flushdata (string JSON) {byte [] jsonbytes; try {jsonbytes = JSON. getbytes ("UTF-8"); response. setcontentlength (jsonbytes. length); response. getoutputstream (). write (jsonbytes); response. getoutputstream (). flush ();} catch (unsupportedencodingexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {response. getoutputstream (). close ();} catch (ioexception e) {e. printstacktrace ();}}}}
Webpage Ajax request:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
Android request data:
Package COM. ZTE. android. greenweb. launcher. HTTP; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. unsupportedencodingexception; import java.net. httpurlconnection; import java.net. URL; import COM. ZTE. android. greenweb. launcher. util. logex; import COM. ZTE. android. greenweb. launcher. util. netconst; public class httpconnectionclient {public static final string http_request_method_get = "get"; public static final string http_request_method_post = "Post"; private httpurlconnection conn = NULL; /*** send the request to the HTTP service and then receive the returned message ** @ Param path * The request's HTTP service path * @ return returns the request's response information * @ throws ioexception */Public int doget (string path) throws ioexception {URL url = new URL (PATH); // openconnection () returns a urlconnection object, which indicates the connection conn = (httpurlconnection) URL of the remote object referenced by the URL. openconnection (); // open a connection Conn. setrequestmethod (http_request_method_get); // sets the get method to request Conn. setconnecttimeout (netconst. connected_time_out); Conn. setreadtimeout (netconst. read_time_out); // if you plan to use a URL Connection for output, set the dooutput flag to trueconn. setdooutput (true); // only the header fields of the content type and content length are set here based on the transmitted content. // The content type Content-Type: // application/X-WWW-form-urlencoded, text/XML; charset = GBK // Content Length Content-Length: 38conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8"); Conn. setrequestproperty ("charset", "UTF-8"); // Save the Response Message return conn after the HTTP service is called. getresponsecode ();} /*** send the request to the HTTP service and then receive the returned message ** @ Param jsonstr * The request's JSON-format string * @ Param path * The request's HTTP service path * @ return returns request Response Information * @ throws ioexception */Public int dopost (string jsonstr, string path) throws ioexception {logex. D ("dopost request =" + jsonstr); // obtain the binary data byte [] DATA = jsonstr. getbytes (); Url url = new URL (PATH); // open a connection conn = (httpurlconnection) URL. openconnection (); // set the POST method to request Conn. setrequestmethod (http_request_method_post); Conn. setconnecttimeout (netconst. connected_time_out); Conn. setreadtimeout (netconst. read_time_out); // if you plan to use a URL Connection for output, set the dooutput flag to trueconn. setdooutput (true); Conn. setdoinput (true); // only the header fields of the content type and content length are set here based on the transmitted content. // The content type Content-Type: // application/X-WWW-form-urlencoded, text/XML; charset = GBK // Content Length Content-Length: 38conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8"); Conn. setrequestproperty ("charset", "UTF-8"); Conn. setrequestproperty ("Content-Length", String. valueof (data. length); outputstream outstream = Conn. getoutputstream (); // return the output stream written to this connection // write binary data to the output stream outstream. write (data); // the data in the memory is flushed into outstream. flush (); // close the stream outstream. close (); int Ic = 0; Ic = Conn. getresponsecode (); Return IC;} Public String getcontent () throws unsupportedencodingexception, ioexception {If (null = conn) {return NULL ;} // Save the response message string MSG = "" after the HTTP service is called; // if the request response code is 200, if (Conn. getresponsecode () = netconst. net_connected_success) {// The encoding returned by the HTTP server is a UTF-8, so it must be set to a UTF-8 to ensure unified encoding, otherwise there will be Chinese garbled bufferedreader in = new bufferedreader (New inputstreamreader (inputstream) conn. getinputstream (), "UTF-8"); // returns the input stream MSG = in. readline (); In. close ();} // disconnect Conn. disconnect (); logex. D ("dopost getcontent =" + MSG); Return MSG ;}}
Recently I learned a lot about the SSH2 framework. Let's take a look later. Some code references the network.