jeewx-api.jar入门教程

来源:互联网 发布:2017java程序设计竞赛 编辑:程序博客网 时间:2024/05/22 06:18

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页面

Html代码  收藏代码
  1. <body>  
  2. <form action="ApiDemo" method="post">     
  3.     <table>          
  4.         <tr>              
  5.             <td>appId</td>              
  6.             <td><input type="text" id="appId" name="appId" /></td>           
  7.        </tr>          
  8.        <tr>              
  9.             <td>appSecret</td>              
  10.            <td><input type="text" id="appSecret" name="appSecret" /></td>          
  11.        </tr>          
  12.        <tr>              
  13.            <td colspan="2" align="center"><input type="submit" value="提交" /></td>          
  14.        </tr>      
  15.     </table>  
  16. </form>  
  17. <%  
  18. /* 获取后台传过来的内容 */  
  19. String data = (String)session.getAttribute("data");  
  20. %>  
  21. <%=data %>  
  22. </body>  

 

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

6、在doPost方法中,写:

 

 

Java代码  收藏代码
  1.  //设置编码格式          
  2. request.setCharacterEncoding("utf-8");          
  3. //获取页面传过来的参数          
  4. String appid = request.getParameter("appId");          
  5. String appsecret = request.getParameter("appSecret");          
  6. //判断appid和appsecret是否为空          
  7. if(StringUtils.isNotBlank(appid) && StringUtils.isNotBlank(appsecret)){              
  8.     //token              
  9.     String token = null;              
  10.     //获取token              
  11.     try {                  
  12.         //调用jeewx-api中的方法,获取token                  
  13.         token = JwTokenAPI.getAccessToken(appid, appsecret);              
  14.     } catch (WexinReqException e) {              
  15.     }              
  16.     List<Wxuser> users = null;              
  17.     //通过token获取所有用户              
  18.     try {                  
  19.         users = JwUserAPI.getAllWxuser(token, null);              
  20.     } catch (WexinReqException e) {              
  21.     }              
  22.     StringBuffer result = new StringBuffer();              
  23.     //将结果拼成字符串              
  24.     for (Wxuser wxuser : users) {                  
  25.         result.append("国家:"+wxuser.getCountry()+" 城市:"+wxuser.getCity()+" 性别:"+wxuser.getSex()+"<br/>");              
  26.     }              
  27.     //将结果放入session中              
  28.     request.getSession().setAttribute("data", result.toString());              
  29.     //跳转到刚才的页面              
  30.     response.sendRedirect("index.jsp");          
  31. }  


7、web.xml文件中

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">    
  3.     <display-name>apiDemo</display-name>    
  4.     <welcome-file-list>      
  5.         <welcome-file>index.html</welcome-file>      
  6.         <welcome-file>index.htm</welcome-file>      
  7.         <welcome-file>index.jsp</welcome-file>      
  8.         <welcome-file>default.html</welcome-file>      
  9.         <welcome-file>default.htm</welcome-file>      
  10.         <welcome-file>default.jsp</welcome-file>    
  11.     </welcome-file-list>    
  12.     <servlet>      
  13.         <description></description>      
  14.         <display-name>ApiDemo</display-name>      
  15.         <servlet-name>ApiDemo</servlet-name>      
  16.         <servlet-class>apiDemo.userServiceTest.ApiDemo</servlet-class>    
  17.     </servlet>    
  18.     <servlet-mapping>      
  19.         <servlet-name>ApiDemo</servlet-name>      
  20.         <url-pattern>/ApiDemo</url-pattern>    
  21.     </servlet-mapping>  
  22. </web-app>  

 
8、运行tomcat服务器 
页面: 

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

 

0 0
原创粉丝点击