① 微信公众平台 java开发如何在if中回应用户发来的消息
这个,应该考虑一下使用状态机了。根据实际的需要,定义几种状态,在处理用户信息的时候放到状态里去处理,然后再根据用户选择项“1、2、3...”,去进行实际的响应。
否则,你自己要定义太多的MATCH,程序实现起来复杂,用户使用起来也不方便。
② 如何利用Java语言实现消息推送到手机app
首先APP后台就得有这样的轮询程序,比如每次打开app时触发,比如每隔10分钟触发,每次触发就调用下服务器端的服务,服务端去拉取要推送的信息,或者知道对方的手机号或微信号,那就直接调用短信接口或直接发送微信信息了。③ 如何使用微信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 微信开发大神解答下如何响应图文消息
您好,这样的:
1)图文消息的个数限制为10,也就是图中ArticleCount的值(图文消息的个数,限制在10条以内);
2)对于多图文消息,第一条图文的图片显示为大图,其他图文的图片显示为小图;
3)第一条图文的图片大小建议为640*320,其他图文的图片大小建议为80*80;
下面是实例代码:
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
// 接收用户发送的文本消息内容
String content = requestMap.get("Content");
// 创建图文消息
NewsMessage newsMessage = new NewsMessage();
newsMessage.setToUserName(fromUserName);
newsMessage.setFromUserName(toUserName);
newsMessage.setCreateTime(new Date().getTime());
newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
newsMessage.setFuncFlag(0);
List<Article> articleList = new ArrayList<Article>();
// 单图文消息
if ("1".equals(content)) {
Article article = new Article();
article.setTitle("微信公众帐号开发教程Java版");
article.setDescription("柳峰,80后,微信公众帐号开发经验4个月。为帮助初学者入门,特推出此系列教程,也希望借此机会认识更多同行!");
article.setPicUrl("http://0.xiaoqrobot.app.com/images/avatar_liufeng.jpg");
article.setUrl("http://blog.csdn.net/lyq8479");
articleList.add(article);
// 设置图文消息个数
newsMessage.setArticleCount(articleList.size());
// 设置图文消息包含的图文集合
newsMessage.setArticles(articleList);
// 将图文消息对象转换成xml字符串
respMessage = MessageUtil.newsMessageToXml(newsMessage);
}
⑤ java实现微信发送消息
net的我有 java的还没看呢 给你说说原理 通过开发者id 或者关注者列表 然后通过用户openid(用户唯一标示)向用户发送客服消息 他这个通道是走的客服消息 ,前提是必须关注者主动向公众号发过消息 时限为24h
⑥ java实现微信自动领红包收转账的功能应该怎么做
微信可以设置自动收红包转账方法如下:
1、打开你的微信,你就可以看朋友的头像发来的转账消息,请你确认收钱。
2、点击打开消息,可以看到转账金额,点击进入确认收款。
3、若一天内未及时收款,就会退回给转账方。
⑦ 微信怎么接收消息通知。
1.进入微信主界面点击”我“,接着点击设置。
2..在设置界面点击新消息提醒选项。
3.进入勾选接收新消息通知即可。打开登录微信,点击底部菜单“我”,点击手机设置打开。
4.然后选择退出微信,在即将退出的弹框将退出后接收微信消息弹框选择即可。
5.再次收到微信消息就会有提醒消息了。
⑧ java怎么获取微信发来的消息内容
这个,挺麻烦了
~~~~~
⑨ JAVA微信公众号开发回复消息能回复多条吗具体怎么代码实现
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 接收参数微信加密签名、 时间戳、随机数
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
PrintWriter out = response.getWriter();
// 请求校验
boolean checkSignature = SignUtil.checkSignature(signature, timestamp, nonce);
if (checkSignature) {
// 调用核心服务类接收处理请求
String respXml = processRequest(request);
out.print(respXml);
}
out.close();
out = null;
}