ããå¨androidä¸ææ¶åæ们ä¸éè¦ç¨å°æ¬æºçSQLiteæ°æ®åºæä¾æ°æ®ï¼æ´å¤çæ¶åæ¯ä»ç½ç»ä¸è·åæ°æ®ï¼é£ä¹Androidæä¹ä»æå¡å¨ç«¯è·åæ°æ®å¢ï¼æå¾å¤ç§ï¼å½çº³èµ·æ¥æ
ããä¸ï¼åºäºHttpåè®®è·åæ°æ®æ¹æ³ãäºï¼åºäºSAOPåè®®è·åæ°æ®æ¹æ³ï¼ä¸ï¼å¿äº-------
ããé£ä¹æ们çè¿ç¯æç« ä¸»è¦æ¯å°å
³äºä½¿ç¨Httpåè®®è·åæå¡å¨ç«¯æ°æ®ï¼è¿éæ们éåçæå¡å¨ç«¯ææ¯ä¸ºjavaï¼æ¡æ¶ä¸ºStruts2,æè
å¯ä»¥æServlet,åæè
å¯ç´æ¥ä»JSP页é¢ä¸è·åæ°æ®ã
ããé£ä¹ï¼æ¥ä¸æ¥æ们便å¼å§è¿ä¸è·¯ç¨ï¼
ããé¦å
ï¼ç¼åæå¡å¨ç«¯æ¹æ³,æè¿ééç¨çMVCæ¡æ¶æ¯Struts2ï¼ç®çå¾å纯ï¼å°±æ¯ä¸ºäºä»¥åå个å®æ´çåä¸é¡¹ç®ï¼ææ¯é
å¤ä¸ºï¼android+SSHãå½ç¶ï¼ç¯å¹
æéï¼æè¿éå°±ç´æ¥ç¨Strtus2èå·²ã
ããæå¡å¨ç«¯ï¼æ°å»ºWebProject ,éæ©Java ee 5.0.
ãã为äºç»é¡¹ç®æ·»å Struts2çæ¯æï¼æ们å¿
须导å
¥Struts2çä¸äºç±»åºï¼å¦ä¸å³å¯ï¼æäºjarå
æ¯ä¸å¿
çï¼ä½æ¯æ们åæ¥æ©å±å¯è½æ¯è¦ä½¿ç¨å°çï¼å°±å
å¼è¿å»ï¼ï¼
ãã1: xwork-core-2.2.1.1.jar
ãã2: struts2-core-2.2.1.1.jar
ãã3: commons-logging-1.0.4.jar
ãã4: freemarker-2.3.16.jar
ãã5: ognl-3.0.jar
ãã6: javassist-3.7.ga.jar
ãã7ï¼commons-ileupload.jar
ãã8ï¼commons-io.jar
ãã9ï¼json-lib-2.1-jdk15.jar å¤çJSONæ ¼å¼æ°æ®è¦ä½¿ç¨å°
ãã10ï¼struts2-json-plugin-2.2.1.1.jar åºäºstruts2çjsonæ件
ãã以ä¸çjarå
ï¼éè¦æ¾å¨WebRoot/WEB-INF/libç®å½ä¸
ããç¶åå¨web.xmlæ件ä¸æ²ä¸ï¼
ããView Code
ãã<?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">
<!-- å®ä¹Struts2çæ ¸å¿æ§å¶å¨ï¼FilterDispatcher -->
<filter>
<!-- å®ä¹æ ¸å¿Filterçå称 -->
<filter-name>struts2</filter-name>
<!-- å®ä¹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æ件ï¼å¹¶æ¾å¨WebRoot/WEB-INF/libç®å½ä¸:å¦ä¸ä»£ç ï¼
ããView Code
ãã<?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>
<!-- setting encoding,DynamicMethod,language
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- add package here extends="struts-default"-->
<package name="dongzi" extends="json-default"> <!--éè¦å°struts-defaultæ¹ä¸ºjson-default-->
<!-- setting action -->
<action name="login" class="com.dongzi.action.loginAction" method="login">
<result type="json"></result> <!--è¿åå¼ç±»å设置为json,ä¸è®¾ç½®è¿å页é¢-->
</action>
</package>
</struts>
ããé
置好åï¼æ们åæ ¹æ®<action>æ ç¾å
容æ¥ç¼åactionãæ¹æ³ä¸ºmethod对åºçloginï¼ç±»å为loginAction,
ãã注æï¼å
继æ¿ä¸ºï¼json-default ï¼è¾åºç»æç±»å为json
ããå¦ä¸ï¼
ããView Code
ããpublic class loginAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
HttpServletRequest request;
HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
public void login(){
try {
//HttpServletRequest request =ServletActionContext.getRequest();
// HttpServletResponse response=ServletActionContext.getResponse();
this.response.setContentType("text/html;charset=utf-8");
this.response.setCharacterEncoding("UTF-8");
if(this.request.getParameter("username").equals("123456")){
this.response.getWriter().write("ççå¾å¥æª,æ¥æ¬äººï¼");
}else if(this.request.getParameter("username").equals("zhd")){
this.response.getWriter().write("没æéï¼æå°±æ¯ä¸åå¥ï¼");
}else{
this.response.getWriter().write("æå°±æ¯ä¸åå¥ï¼");
}
//å°è¦è¿åçå®ä½å¯¹è±¡è¿è¡jsonå¤ç
// JSONObject json=JSONObject.fromObject(this.getUsername());
//è¾åºæ ¼å¼å¦ï¼{"id":1, "username":"zhangsan", "pwd":"123"}
// System.out.println(json);
// this.response.getWriter().write(json.toString());
/**
JSONObject json=new JSONObject();
json.put("login", "login");
response.setContentType("text/html;charset=utf-8");
System.out.println(json);
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
**/
/**
JSONObject json=new JSONObject();
json.put("login", "login");
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
**/
} catch (Exception e) {
e.printStackTrace();
}
// return null;
}
}
ããè¿è¡æ¥çä¸ï¼
http://localhost:8080/PDAServer/login.action?username=123456 å½ç¶ä½ å¯ä»¥è¾å
¥å
¶ä»åæ°çURL