[微信机器人_06]编码实现(完结)

来源:互联网 发布:在淘宝网上怎么退货 编辑:程序博客网 时间:2024/04/27 15:15

代码请到此处下载:http://download.csdn.net/detail/elcarim/7037573


说明:由于自己最开始学习java时不知道如何打开下载的java工程,这里特别说明一下。下载java代码后,如果文件夹中有".classpath”和” .project”文件,则可以以工程的形式打开盖源码。方法如下:

File → Import → General → ExisitingProject into Workspace → Next → Select root Directory → Finish

               


进入正题。

刚接触java不久,代码本身实现的很简单,贴代码没啥意义,主要从初学者的角度说明一些开发中碰到的问题,关于流程性的东西可参考前面几篇文档。

 

代码结构

包名

说明

org.miracle.dao              

数据访问对象(DAO)层

org.miracle.database         

数据库连接和基本操作管理

org.miracle.helper

本地编译百度api使用

org.miralce.robot.api

访问外部api服务,如天气、音乐等

org.miralce.robot.nlp

自然语言处理模块

org.miralce.robot.service

主业务模块,会话管理

org.miralce.robot.servlet

程序入口

org.miralce.wx.adptor

微信适配层

org.miralce.wx.request.bean

来自微信服务器的请求数据封装

org.miralce.wx.response.bean

发送给微信服务器的响应数据封装

 

外部库如何引入

由于需要xml、json格式解析,以及mysql数据库访问,所以需要引入一些外部的jar文件。如xml用到的xstream-1.3.1.jar和dom4j-1.6.1.jar。

下载该jar文件后直接拷贝到WebContent/WEB-INF/lib下即可。

 

如何在本地调试程序

不可能没写一个小功能就发布到服务器上调试,所以必须在本地做一些单元测试。最简单的方法:

在代码中定义一个main函数,将单元测试用例放在里面。然后选择Run As → Java Application执行,即可在本地进行测试。可参考代码中的WeiXinAdaptor.java实现。

 

关于gson的使用

程序中使用了gson库来做json格式的转换。但是在使用中碰到了一个令我十分困扰的问题,在使用过程中抛出如下异常:

Registeran InstanceCreator with Gson for this type to fix this problem

查了很多博客都没讲清楚,还是翻了原始的用户手册才弄明白,即:

gson不支持内部类的反序列化,可以将内部类定义为静态的(在类前加static),或者为该内部类定义一个构造函数,但是作者不建议使用后者,还是使用静态类比较好。

 

原文如下:

Nested Classes (including Inner Classes)

Gson can serialize static nested classesquite easily. 

Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization.You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it. Here is an example: 

public class A {    public String a;       class B {       public String b;              public B() {          // No args constructor for B        }    }}

NOTE: Theabove class B can not (by default) be serialized with Gson.

Gson can not deserialize {"b":"abc"} intoan instance of B since the class B is an inner class. if it was defined as static class B then Gson would have been able to deserialize the string. Another solution isto write a custom instance creator for B. 

public class InstanceCreatorForB implementsInstanceCreator<A.B> {    private final A a;    public InstanceCreatorForB(A a)  {        this.a = a;    }    public A.B createInstance(Type type) {        return a.new B();    }}

The above is possible, but not recommended.

 

百度云mysql不支持view

由于百度云的数据库不知道view,在dao层中写了一些很长的sql语句来规避这个问题。使用云服务的数据库还是不太方便,希望以后会越来越好。

 

mysql文件

CREATE TABLE `t_answer` (  `t_answer_id` int(11) NOT NULL AUTO_INCREMENT,  `type` int(11) DEFAULT NULL,  `value` varchar(45) DEFAULT NULL,  PRIMARY KEY (`t_answer_id`)) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
CREATE TABLE `t_category` (  `t_category_id` int(11) NOT NULL AUTO_INCREMENT,  `value` varchar(45) DEFAULT NULL,  `type` varchar(45) DEFAULT NULL,  PRIMARY KEY (`t_category_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `t_tree` (  `t_tree_id` int(11) NOT NULL AUTO_INCREMENT,  `node_id` int(11) DEFAULT NULL,  `parent_id` int(11) DEFAULT NULL,  `answer_id` int(11) DEFAULT NULL,  PRIMARY KEY (`t_tree_id`)) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
CREATE TABLE `t_word` (  `t_word_id` int(11) NOT NULL AUTO_INCREMENT,  `type` int(11) DEFAULT NULL,  `value` varchar(45) DEFAULT NULL,  `height` int(11) DEFAULT NULL,  PRIMARY KEY (`t_word_id`)) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
CREATE TABLE `t_context` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `user` varchar(45) DEFAULT NULL,  `state` varchar(45) DEFAULT NULL,  `last_time` bigint(20) DEFAULT NULL,  `question` text,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

写在最后

互联网应用与底层开发相比,重点在于业务,很多功能都可以使用已有的库来实现,而无需在底层实现上花费大量精力。正是由于编码的高效,互联网应用瞬息万变,各种新的服务、方法层出不穷,码农们需要不断了解各种新技术、新思想来扩展视野,否则稍不留神就被抛在浪潮之后。

 

做这个机器人也许是一时兴起、也许只是源于一个底层码农的寂寞。

后面计划针对个人、中小企业的服务器部署,网站、应用建设做一些研究,但愿能够完成。


谢谢关注奇迹蛋~扫一下&调戏之

4 0
原创粉丝点击