Home  >  Article  >  Java  >  An example of how Java calls WeChat customer service messages to complete shipping notifications (picture)

An example of how Java calls WeChat customer service messages to complete shipping notifications (picture)

黄舟
黄舟Original
2017-08-20 09:12:482851browse

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

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn