阅读背景:

向微信公众号发送么个特地消息给么个人

来源:互联网 

package com.YY.tuling;

import java.io.UnsupportedEncodingException;

import net.sf.json.JSONArray;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.liufeng.course.pojo.Token;

import com.YY.Entity.ReceiveXmlEntity;
import com.YY.util.CommonUtil;
import com.YY.util.FormatXmlResult;

public class HttpGetRequest {

	/**
	 * Post 要求
	 * @param url 要求地址
	 * @param Param 要求内容
	 * @return 接口返回的内容
	 */
	private static String post(String url ,String param){
		try{
			HttpPost request = new HttpPost(url);
			request.setEntity(new StringEntity(param,"UTF-8"));
			HttpResponse response = HttpClients.createDefault().execute(request);
			//依据返回结武断定要求是不是胜利
			if(200 == response.getStatusLine().getStatusCode()){
				return EntityUtils.toString(response.getEntity());
			}
			return "";
		}catch(Exception e){e.printStackTrace();return "";}
	}

	public static String getExction() throws Exception{

		// 第三方用户唯一凭证
		String appId = "APPID";
		// 第三方用户唯一凭证密钥
		String appSecret = "APPSECRET";
		// 调用接口获得凭证
		Token token = CommonUtil.getToken(appId, appSecret);
		String param =FormatXmlResult.getJsonResult();
		String Url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token.getAccessToken();
		String result = post(Url,param);
		System.out.println(result);
		
		return result;
	}
}

<pre name="code" class="java">package com.YY.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.YY.Entity.ReceiveXmlEntity;

/**
 * Json
 * @author Administrator
 *
 */
public class FormatXmlResult {

	public static String getJsonResult() throws UnsupportedEncodingException{
		 // JSON格局数据解析对象 
        JSONObject jo = new JSONObject();
        
        jo.put("touser", "资讯接收者的OPendID");
        jo.put("template_id", "gCC5EseXsJXrLC-mOzUc9EFE3ehSH0hAApMLyWii-io"); 每一个微信大众,号都有的template_id
        jo.put("url", "https://www.sina.com.cn"); //这个是资讯连接,一般动员态连接。
        jo.put("topcolor", "#7B68EE");
        
        JSONObject map11 = new JSONObject();
        map11.put("value", "以下单据须要审批!");
        map11.put("color", "#173177");
        
        JSONObject map12 = new JSONObject();
        map12.put("value", "A1");
        map12.put("color", "#173177");
        
        JSONObject map13 = new JSONObject();
        map13.put("value", "B2");
        map13.put("color", "#173177");
        
        JSONObject map14 = new JSONObject();
        map14.put("value", "C3");
        map14.put("color", "#173177");
        
        JSONObject map15 = new JSONObject();
        map15.put("value", "请尽快审");
        map15.put("color", "#173177");
        
        Employee employee = new Employee();
        employee.setFirst(map11);
        employee.setKeynote1(map12);
        employee.setKeynote2(map13);
        employee.setKeynote3(map14);
        employee.setRemark(map15);
        
        jo.put("data", employee);
		return jo.toString();
	}
}
package com.YY.tuling;

import java.io.Unsupported




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: