① 微信公眾平台 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;
}