jeewx-api.jar入门教程

jeewx-api.jar入门教程
附件:
http://download.csdn.net/detail/zxl78585/8549027
1、到微信官网申请测试帐号
申请地址:http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
点击登录后,用自己的微信扫描



2、在Eclipse中配置Tomcat
在window->preferences->server->runtime environments

2、打开Eclipse,创建一个web项目

我的eclipse默认的jdk是1.5的,这里,我们需要将jdk修改成1.7的。:在项目上点击右键,选择build path,选中configure build path,将libraries中的jdk1.5选中,点击edit,修改成1.7的。
3、导入jar包
jeecg-api包依赖其他九个包,一块儿导入

选中这些包,点击右键,build path ,点击add to build path,这样,所有的jar都被添加到项目中去了
4、创建表单页面
在WebContent目录中创建一个index.jsp页面

<body>
<form action="ApiDemo" method="post">   
    <table>        
        <tr>            
            <td>appId</td>            
            <td><input type="text" id="appId" name="appId" /></td>         
       </tr>        
       <tr>            
            <td>appSecret</td>            
           <td><input type="text" id="appSecret" name="appSecret" /></td>        
       </tr>        
       <tr>            
           <td colspan="2" align="center"><input type="submit" value="提交" /></td>        
       </tr>    
    </table>
</form>
<%
/* 获取后台传过来的内容 */
String data = (String)session.getAttribute("data");
%>
<%=data %>
</body>

 

5、创建servlet
新创建一个servlet会报错,这个一般是缺少jar导致的,这时候,我们不需要从外部导入,在build path中将tomcat的包加过来即可添加方法
在项目上点击右键
build path-&gt;configure build path-&gt;libraries-&gt;add library-&gt;server runtime

6、在doPost方法中,写:

 //设置编码格式        
request.setCharacterEncoding("utf-8");        
//获取页面传过来的参数        
String appid = request.getParameter("appId");        
String appsecret = request.getParameter("appSecret");        
//判断appid和appsecret是否为空        
if(StringUtils.isNotBlank(appid) && StringUtils.isNotBlank(appsecret)){            
    //token            
    String token = null;            
    //获取token            
    try {                
        //调用jeewx-api中的方法,获取token                
        token = JwTokenAPI.getAccessToken(appid, appsecret);            
    } catch (WexinReqException e) {            
    }            
    List<Wxuser> users = null;            
    //通过token获取所有用户            
    try {                
        users = JwUserAPI.getAllWxuser(token, null);            
    } catch (WexinReqException e) {            
    }            
    StringBuffer result = new StringBuffer();            
    //将结果拼成字符串            
    for (Wxuser wxuser : users) {                
        result.append("国家:"+wxuser.getCountry()+" 城市:"+wxuser.getCity()+" 性别:"+wxuser.getSex()+"<br/>");            
    }            
    //将结果放入session中            
    request.getSession().setAttribute("data", result.toString());            
    //跳转到刚才的页面            
    response.sendRedirect("index.jsp");        
}


7、web.xml文件中

<?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_2_5.xsd" id="WebApp_ID" version="2.5">  
    <display-name>apiDemo</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>    
        <description></description>    
        <display-name>ApiDemo</display-name>    
        <servlet-name>ApiDemo</servlet-name>    
        <servlet-class>apiDemo.userServiceTest.ApiDemo</servlet-class>  
    </servlet>  
    <servlet-mapping>    
        <servlet-name>ApiDemo</servlet-name>    
        <url-pattern>/ApiDemo</url-pattern>  
    </servlet-mapping>
</web-app>

 
8、运行tomcat服务器
页面:

在里面输入appId和appSecret点击提交,下面会返回对应的结果:

猜你喜欢

转载自zxl78585.iteye.com/blog/2197664