java微信开发教程
⑴ 如何使用微信sdk java版
1.首先我们新建一个Java开发包WeiXinSDK
2.包路径:com.ansitech.weixin.sdk
测试的前提条件:
假如我的公众账号微信号为:vzhanqun
我的服务器地址为:http://www.vzhanqun.com/
下面我们需要新建一个URL的请求地址
我们新建一个Servlet来验证URL的真实性,具体接口参考
http://mp.weixin.qq.com/wiki/index.php?title=接入指南
3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java
这里我们主要是获取微信服务器法师的验证信息,具体验证代码如下
[java] view plain print?
package com.ansitech.weixin.sdk;
import com.ansitech.weixin.sdk.util.SHA1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinUrlFilter implements Filter {
//这个Token是给微信开发者接入时填的
//可以是任意英文字母或数字,长度为3-32字符
private static String Token = "vzhanqun1234567890";
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("WeixinUrlFilter启动成功!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
boolean isGet = request.getMethod().toLowerCase().equals("get");
System.out.println("获得微信请求:" + request.getMethod() + " 方式");
if (isGet) {
//验证URL真实性
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce");// 随机数
String echostr = request.getParameter("echostr");//随机字符串
List<String> params = new ArrayList<String>();
params.add(Token);
params.add(timestamp);
params.add(nonce);
//1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(params, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//2. 将三个参数字符串拼接成一个字符串进行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
if (temp.equals(signature)) {
response.getWriter().write(echostr);
}
} else {
//处理接收消息
}
}
@Override
public void destroy() {
}
}
好了,不过这里有个SHA1算法,我这里也把SHA1算法的源码给贴出来吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
[java] view plain print?
/*
* 微信公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
* http://www.ansitech.com/weixin/sdk/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ansitech.weixin.sdk.util;
import java.security.MessageDigest;
/**
* <p>Title: SHA1算法</p>
*
* @author qsyang<[email protected]>
*/
public final class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
5.把这个Servlet配置到web.xml中
[html] view plain print?
<filter>
<description>微信消息接入接口</description>
<filter-name>WeixinUrlFilter</filter-name>
<filter-class>com.ansitech.weixin.sdk.WeixinUrlFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WeixinUrlFilter</filter-name>
<url-pattern>/api/vzhanqun</url-pattern>
</filter-mapping>
好了,接入的开发代码已经完成。
6.下面就把地址URL和密钥Token填入到微信申请成为开发者模式中吧。
⑵ 怎样用java开发微信
java公众号不需要特殊的架构 ,
从最原始的servlet到流行的ssh ssm框架都可以做 ,
后端通过网络请求微信的接口,
从而获得请求的数据 ,
前端可以使用各种前端框架实现,
比如easyui或者bootstrap都可以,
微信开发文档中有详细的示例 。
⑶ 【求助】java微信平台开发
如上回答。
你先确定你的接口能背调用到。
你返回的数据格式是否正确。
⑷ 微信小程序开发怎么做
您好,
微信小程序,开发之前必须要完成和注册认证。如果是个人或者小公司想开发微信小程序,也可以找微信认证第三方开发商,比如赢在移动、正品科技等。
1、微信小程序注册
在微信公众平台官网首页,按照提示点击右上角的“立即注册”按钮,里面总过有12步,按照要求提交就可以了。
2、小程序申请微信认证
政府、媒体、其他组织类型帐号,必须通过微信认证验证主体身份。企业类型帐号,可以根据需要确定是否申请微信认证。已认证帐号可使用微信支付、微信卡券等高级权限。
认证入口:登录小程序—设置—基本设置—微信认证—详情
3、小程序申请微信支付
已认证的小程序可申请微信支付。
4、小程序绑定微信开放平台帐号
小程序绑定微信开放平台帐号后,可与帐号下的其他移动应用、网站应用及公众号打通,通过UnionID机制满足在多个应用和公众号之间统一用户帐号的需求。
UnionID机制说明:如果开发者拥有多个移动应用、网站应用、和公众帐号(包括小程序),可通过UnionID来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号(包括小程序),用户的unionid是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,UnionID是相同的。用户的UnionID可通过调用“获取用户信息”接口获取。
5、了解“获取用户信息”接口请查看开发文档—API—开放接口—用户信息。
绑定小程序流程说明:登录微信开放平台、—管理中心—公众帐号—绑定公众帐号
注意:微信开放平台帐号必须完成开发者资质认证才可以绑定小程序。
⑸ 如何使用java设计一个微信小程序
Java不能设计微信小程序,微信小程序开发有其自己的语言和文件形式。Java可以作为微信小程序的服务端语言和api接口服务器语言为小程序端提供服务,而不是直接开发微信小程序。
⑹ 如何用java开发微信
说明:
本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
[java]view plain
packagedemo.servlet;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importdemo.process.WechatProcess;
/**
*微信服务端收发消息接口
*
*@authorpamchen-1
*
*/
{
/**
*ThedoGetmethodoftheservlet.<br>
*
*.
*
*@paramrequest
*
*@paramresponse
*
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
/**读取接收到的xml消息*/
StringBuffersb=newStringBuffer();
InputStreamis=request.getInputStream();
InputStreamReaderisr=newInputStreamReader(is,"UTF-8");
BufferedReaderbr=newBufferedReader(isr);
Strings="";
while((s=br.readLine())!=null){
sb.append(s);
}
Stringxml=sb.toString();//次即为接收到微信端发送过来的xml数据
Stringresult="";
/**判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回*/
Stringechostr=request.getParameter("echostr");
if(echostr!=null&&echostr.length()>1){
result=echostr;
}else{
//正常的微信处理流程
result=newWechatProcess().processWechatMag(xml);
}
try{
OutputStreamos=response.getOutputStream();
os.write(result.getBytes("UTF-8"));
os.flush();
os.close();
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*ThedoPostmethodoftheservlet.<br>
*
*
*post.
*
*@paramrequest
*
*@paramresponse
*
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description></description>
<display-name></display-name>
<servlet-name>WechatServlet</servlet-name>
<servlet-class>demo.servlet.WechatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WechatServlet</servlet-name>
<url-pattern>/wechat.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
packagedemo.process;
importjava.util.Date;
importdemo.entity.ReceiveXmlEntity;
/**
*微信xml消息处理流程逻辑类
*@authorpamchen-1
*
*/
publicclassWechatProcess{
/**
*解析处理xml、获取智能回复结果(通过图灵机器人api接口)
*@paramxml接收到的微信数据
*@return最终的解析结果(xml格式数据)
*/
publicStringprocessWechatMag(Stringxml){
/**解析xml数据*/
ReceiveXmlEntityxmlEntity=newReceiveXmlProcess().getMsgEntity(xml);
/**以文本消息为例,调用图灵机器人api接口,获取回复内容*/
Stringresult="";
if("text".endsWith(xmlEntity.getMsgType())){
result=newTulingApiProcess().getTulingResult(xmlEntity.getContent());
}
/**此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容
*因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息
**/
result=newFormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(),xmlEntity.getToUserName(),result);
returnresult;
}
}
packagedemo.entity;
/**
*接收到的微信xml实体类
*@authorpamchen-1
*
*/
publicclassReceiveXmlEntity{
privateStringToUserName="";
privateStringFromUserName="";
privateStringCreateTime="";
privateStringMsgType="";
privateStringMsgId="";
privateStringEvent="";
privateStringEventKey="";
privateStringTicket="";
privateStringLatitude="";
privateStringLongitude="";
privateStringPrecision="";
privateStringPicUrl="";
privateStringMediaId="";
privateStringTitle="";
privateStringDescription="";
privateStringUrl="";
privateStringLocation_X="";
privateStringLocation_Y="";
privateStringScale="";
privateStringLabel="";
privateStringContent="";
privateStringFormat="";
privateStringRecognition="";
publicStringgetRecognition(){
returnRecognition;
}
publicvoidsetRecognition(Stringrecognition){
Recognition=recognition;
}
publicStringgetFormat(){
returnFormat;
}
publicvoidsetFormat(Stringformat){
Format=format;
}
publicStringgetContent(){
returnContent;
}
publicvoidsetContent(Stringcontent){
Content=content;
}
publicStringgetLocation_X(){
returnLocation_X;
}
publicvoidsetLocation_X(StringlocationX){
Location_X=locationX;
}
publicStringgetLocation_Y(){
returnLocation_Y;
}
publicvoidsetLocation_Y(StringlocationY){
Location_Y=locationY;
}
publicStringgetScale(){
returnScale;
}
publicvoidsetScale(Stringscale){
Scale=scale;
}
publicStringgetLabel(){
returnLabel;
}
publicvoidsetLabel(Stringlabel){
Label=label;
}
publicStringgetTitle(){
returnTitle;
}
publicvoidsetTitle(Stringtitle){
Title=title;
}
publicStringgetDescription(){
returnDescription;
}
publicvoidsetDescription(Stringdescription){
Description=description;
}
publicStringgetUrl(){
returnUrl;
}
publicvoidsetUrl(Stringurl){
Url=url;
}
publicStringgetPicUrl(){
returnPicUrl;
}
publicvoidsetPicUrl(StringpicUrl){
PicUrl=picUrl;
}
publicStringgetMediaId(){
returnMediaId;
}
publicvoidsetMediaId(StringmediaId){
MediaId=mediaId;
}
publicStringgetEventKey(){
returnEventKey;
}
publicvoidsetEventKey(StringeventKey){
EventKey=eventKey;
}
publicStringgetTicket(){
returnTicket;
}
publicvoidsetTicket(Stringticket){
Ticket=ticket;
}
publicStringgetLatitude(){
returnLatitude;
}
publicvoidsetLatitude(Stringlatitude){
Latitude=latitude;
}
publicStringgetLongitude(){
returnLongitude;
}
publicvoidsetLongitude(Stringlongitude){
Longitude=longitude;
}
publicStringgetPrecision(){
returnPrecision;
}
publicvoidsetPrecision(Stringprecision){
Precision=precision;
}
publicStringgetEvent(){
returnEvent;
}
publicvoidsetEvent(Stringevent){
Event=event;
}
publicStringgetMsgId(){
returnMsgId;
}
publicvoidsetMsgId(StringmsgId){
MsgId=msgId;
}
publicStringgetToUserName(){
returnToUserName;
}
publicvoidsetToUserName(StringtoUserName){
1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do
[html]view plain
1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。
下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。
[java]view plain
2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:
[java]view plain
⑺ 用Java怎么实现微信支付
具体方法步骤:
一、准备阶段:已认证微信号,且通过微信支付认证,这个可以看微信文档,很详细,这里就不再重复。
⑻ 【Java】微信公众平台开发视频教程【共8G】
uni
链接: https://pan..com/s/1ad1Y2dX4VCJ0eIPSiuC5dQ
若资源有问题欢迎追问~
⑼ 如何进行JAVA的微信订餐系统开发
其实么微信开发也就是调用他的API获取用户的操作内容
其实可以先抛开微信,先把后台和功能做好
我之前做个订餐系统,是有个硬件需要购买的,这个硬件可以打印订单和提醒。
硬件厂家会提供接口给你的,用户购买成功给这个接口穿参数,硬件就可以接收后打印订单了同时也有短信提醒。
当系统做好了,只需要熟悉一下微信的接口,可以获取用户输入的内容,根据用户的内容获取系统的订餐信息返回到微信窗口即可
比如 : 你这边推送 套餐信息,用户输入套餐编号,你根据编号去查询这个套餐信息,调用硬件打印方法和短信提醒方法再返回信息到微信窗口给用户,大概的流程就是这样的。
其实这涉及到的接口真的很少的,跟你开发语言也没关系。
⑽ 微信java开发,开发标准是什么
java微信开发应具备的前提条件1 掌握xml解析工具Dom4j、Jdom中的任意一种微信所有的消息处理都是xml,因此xml的解析就显得尤为重要,这集中体现在文本消息、图文消息这两个部分2 掌握JSON开发工具类如json-libjson数据的处理在微信开发集中体现在自定义菜单接口、获取Access_Token、Oauth2.0网页授权等常用接口,此外第三方接口也会使用到如网络翻译、网络词典等。3 掌握xstreamxstream的用途集中体现在java对象转xml字符串这个方面,使用xstream主要是为了最大程度地发挥java面向对象的特点。4 熟悉MD5和SHA-1加密算法加密算法 主要用于微信验证签名和生成签名(微信支付)两个部分5 掌握HTTPConnection和HTTPSConnecion这个部分一帮的第二点配合使用以达到最佳效果6 掌握常用数据库7 能熟练使用linux操作系统