项目需要,兼职搞起了后台,完全木有学过,从头开始,一张白纸,整了很多弯路,随手记下一些知识,也许还会用得着...
环境:Ubuntu 14.04 + Eclipse-JEE-Luna + 红米1s(android 4.3)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>TestWebServer</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置以下内容 --> <servlet> <servlet-name>nihao</servlet-name> <!-- 名称随意,与mapping对应即可 --> <servlet-class>org.lynxz.test.ActionDistribute</servlet-class> <!-- 完成的包名 + 类名 --> </servlet> <servlet-mapping> <servlet-name>nihao</servlet-name> <!-- 与上面的name对应即可 --> <url-pattern>/* </url-pattern> <!-- 访问网址,这里将任意请求都转发给统一处理类 --> </servlet-mapping> </web-app>
package org.lynxz.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author lynxz * @date Sep 21, 2014 * 用于分类处理所有的客户端请求,通过提取请求码,转交给相应的程序 */ public class ActionDistribute extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("有新的get请求,将转交给doPost处理..."); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("有新的post请求需要处理"); String resultJson = "";// 用于返回给客户端的json数据 String actionType = req.getParameter("actionTypeCmd"); if (actionType != null) { int actionCmd = Integer.valueOf(actionType); resultJson = GateWay.getResult(actionCmd, req); } else { resultJson = "{\"status\":\"fail\",\"msg\":\"error requet actionType\"}"; } // 将结果返回给客户端 PrintWriter out = resp.getWriter(); out.write(resultJson); out.flush(); out.close(); } }
package org.lynxz.test; import javax.servlet.http.HttpServletRequest; import org.lynxz.processer.Hello; /** * @author lynxz * @date Sep 21, 2014 * 根据请求码分配处理程序,并返回结果String */ public class GateWay { private static GeneralService service = null; public static String getResult(int actionCmd, HttpServletRequest req) { switch (actionCmd) { case 1: service = new Hello(); break; default: break; } return service == null ? null : service.postBack(); } }
package org.lynxz.test; public interface GeneralService { public String postBack(); }
package org.lynxz.processer; import org.lynxz.test.GeneralService; /** * @author lynxz * @date Sep 21, 2014 * 实际处理客户端请求的具体程序 * 由于通讯传输的时候文本,所有程序均要返回String, * 因此抽象一个接口便于各个程序实现 */ public class Hello implements GeneralService { @Override public String postBack() { // json内容随意,与客户端配合来编写 String json = "{\"status\":\"success\",\"msg\":\"hello client\"}"; return json; } }之后运行该项目:run as ==> run on server,即可在浏览器中看到结果:
aq.ajax(String url, Map<String, ?> params, Class<TestEntity> type, Object handler, String callback)
Map<String, String> params = null;
/* * 设置用于解析服务器返回值的转换类, * GsonTransformer实现了接口com.androidquery.callback.Transformer; */ AbstractAjaxCallback.setTransformer(new GsonTransformer()); AbstractAjaxCallback.setTimeout(3000); AQuery aq = new AQuery(this); params = new HashMap<String, String>(); params.put("actionTypeCmd", "1"); params.put("content", "hello"); aq.ajax(url, params, TestEntity.class, this, "helloCallback");其中"TestEntity"是自定义类,aq会将服务器返回的数据解析成该类,而"helloCallback"是回调方法,服务器返回或超时后进行调用,格式如下:
public void helloCallback(String url, TestEntity json, AjaxStatus status) {......}
package com.example.test; public class TestEntity { String status; String msg; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }GsonTransformer类:
package com.example.test; import com.androidquery.callback.AjaxStatus; import com.androidquery.callback.Transformer; import com.google.gson.Gson; /** * @author lynxz * @date Sep 21, 2014 * 用于将服务器返回的数据转换成本地类,便于调用 */ public class GsonTransformer implements Transformer { Gson gson = new Gson(); @Override public <T> T transform(String url, Class<T> type, String encoding, byte[] data, AjaxStatus status) { System.out.println("从服务器获得的数据 = " + new String(data)); return gson.fromJson(new String(data), type); } }MainActivity类:
package com.example.test; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.os.Bundle; import com.androidquery.AQuery; import com.androidquery.callback.AbstractAjaxCallback; import com.androidquery.callback.AjaxStatus; public class MainActivity extends Activity { private String url = "http://192.168.0.103:8080/TestWebServer/hello"; private Map<String, String> params = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 设置用于解析服务器返回值的转换类, * GsonTransformer实现了接口com.androidquery.callback.Transformer; */ AbstractAjaxCallback.setTransformer(new GsonTransformer()); AbstractAjaxCallback.setTimeout(3000); AQuery aq = new AQuery(this); params = new HashMap<String, String>(); params.put("actionTypeCmd", "1"); aq.ajax(url, params, TestEntity.class, this, "helloCallback"); } public void helloCallback(String url, TestEntity json, AjaxStatus status) { System.out.println("进入回调方法"); if (json != null) { System.out.println("status = " + json.getStatus()); System.out.println("msg = " + json.getMsg()); } else { System.out.println("json == null"); } } }