项目需要,兼职搞起了后台,完全木有学过,从头开始,一张白纸,整了很多弯路,随手记下一些知识,也许还会用得着...
环境:Ubuntu 14.04 + Eclipse-JEE-Luna + 红米1s(android 4.3)
服务端
创建项目
- 下载tomcat 安装版跟解压缩版本我试了都可以用,这里用后者,下完压缩包,解压即可,我都是懒得处理权限,所以解压到了home目录;
- 在Eclipse中新建项目" web ==> dynamic web project",在下一步的"target runtime"中选择"new runtime",就有如下界面:
name随便起,然后选择刚才解压的tomcat目录,finish即可;
当然,runtime也可以通过" window ==> preferences ==> server ==> runtime"中进行创建/修改等操作;
最后,记得生成web.xml文件:
编写服务端处理程序
- 这里采用比较简单的servlet,架构如下:
- a.所有请求均提交给分配类ActionDistribute;
- b.分配类再根据请求信息中的命令编码int cmd,去调用相应的处理类,并返回String;
- web.xml配置:
- ActionDistribute分配类:
- GateWay网关分配类:
- 通用返回接口GeneralService:
- 具体的处理程序:
- 之后运行该项目:run as ==> run on server,即可在浏览器中看到结果:
- 网址最后面可以加任意字符,服务端创建到此告一段落;
<?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;
}
}
客户端与服务器的通讯方法有多种,这里先记录的是AQuery,可参考此文,用到的query和gson库可以到网盘下载,Demo文件下载:Demo03:
AQuery
- 新建项目,设置权限等操作就省略了,这里是调用到的方法是:
- 客户端通过aquery将包含请求信息的map发送给url指向的服务器,服务器响应并返回json数据,客户端利用transformer及gson将gson解析成具体的类以方便客户端调用;
- AQuery的使用方法:
- 其中"TestEntity"是自定义类,aq会将服务器返回的数据解析成该类,而"helloCallback"是回调方法,服务器返回或超时后进行调用,格式如下:
- 具体内容:
- TestEntity类:
- GsonTransformer类:
- MainActivity类:
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");
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;
}
}
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);
}
}
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");
}
}
}