


This article mainly introduces the method of using Java to call WeChat customer service messages to implement delivery notifications. It analyzes in detail the principles, calling methods and related precautions of Java for WeChat interface calls in the form of examples. Friends in need can refer to the following
The example in this article describes how Java calls WeChat customer service messages to implement delivery notifications. Share it with everyone for your reference, the details are as follows:
Personal note: This is a sample. There are many kinds of WeChat customer messages. I am currently using the official account to send messages. It looks like the picture below.
#Explanation: The code part begins below.
1. First read the WeChat documentation. Here is what we need
This is to say that sending a message requires a POST request to this interface: https://api.weixin.qq.com/cgi-bin/message/ custom/send?access_token=ACCESS_TOKEN
But this interface needs to be followed by a parameter ACCESS_TOKEN.
Get ACCESS_TOKEN first.
//这里的WeixinUtil.getAccess_token()方法,放在下面。 String aToken = WeixinUtil.getAccess_token("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+你的appId+"&secret="+你的appSecret+""); System.out.println("这里是aToken"+aToken); String[] tokenOne = aToken.split(":"); String[] token = tokenOne[1].split(","); char [] stringArr = token[0].toCharArray(); String token3 = "" ; for(int i=1;i<stringArr.length-1;i++){ String token2 = String.valueOf(stringArr[i]); token3 += token2; } System.out.println("这里是ACCESS_TOKEN"+token3);
Get an ACCESS_TOKEN, and then add it to the WeChat request
//这里就是一个微信请求,首先用String放着 String tokenurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+token3; //首先确定是发送文字消息,还是图文消息,这里是手写的json数据. //发送文字消息,无连接 String json = "{\"touser\":\"这里是Openid\",\"msgtype\":\"text\",\"text\":{\"content\":\"Hello World\"}}"; //图文消息,有链接连接 String jsonpic = "{\"touser\":\""+这里是Openid+"\","+ "\"msgtype\":\"news\",\"news\":{\"articles\":["+ "{\"title\":\"HelloWorld\",\"url\":\"要跳转的链接"}]}}"; System.out.println("这里是json"+jsonpic); //请求方法,然后放回OK 成功,否则错误。这里这个请求方法在下边 String xmlStr = HttpKit.post(tokenurl,jsonpic); System.out.println("这里是xmlStr"+xmlStr);
Instructions: WeixinUtil.getAccess_token()
method. I put the whole class. To change the package name, you only need to import two packages
package com.uitrs.weixin; import java.net.HttpURLConnection; import java.net.URL; public class WeixinUtil { //传入URL public static String getAccess_token(String url) { String accessToken = null; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet .openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); accessToken = new String(jsonBytes, "UTF-8"); System.out.println(accessToken); is.close(); } catch (Exception e) { e.printStackTrace(); } return accessToken; } }
Description: HttpKit.post();
method, I put the entire class. I use this class to import the
import com.jfinal.kit.HttpKit;
package into the jfinal package. It comes from the following three packages, I don’t know the details
1.jfinal-2.2.jar (should be this one)
2.jfinal-2.2-bin-with-src.jar
3.jfinal-weixin-1.7-bin-with-src.jar
The above is the detailed content of An example of how Java calls WeChat customer service messages to complete shipping notifications (picture). For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
