JAVA微信开发:[20]如何实现主动发送微信消息

/**

* @param content 文本内容

* @return

*/

public void sendTextMessageToUser(String content,String toUser){

String json = "{"touser": ""+toUser+"","msgtype": "text", "text": {"content": ""+content+""}}";

//获取access_token

GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();

String accessToken = getExistAccessToken.getExistAccessToken();

//获取请求路径

System.out.println("json:"+json);

try {

connectWeiXinInterface(action,json);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* @param mediaId 图片或者语音内容

* @param messageType 消息类型

* @return

*/

public void sendPicOrVoiceMessageToUser(String mediaId,String toUser,String msgType){

String json=null;

if(msgType.equals(REQ_MESSAGE_TYPE_IMAGE)){

json = "{"touser": ""+toUser+"","msgtype": "image", "image": {"media_id": ""+mediaId+""}}";

}else if(msgType.equals(REQ_MESSAGE_TYPE_VOICE)){

json = "{"touser": ""+toUser+"","msgtype": "voice", "voice": {"media_id": ""+mediaId+""}}";

}

//获取access_token

GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();

String accessToken = getExistAccessToken.getExistAccessToken();

//获取请求路径

try {

connectWeiXinInterface(action,json);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 发送图文给所有的用户

* @param openId 用户的id

*/

public void sendNewsToUser(String openId){

MediaUtil mediaUtil = MediaUtil.getInstance();

ArrayList<Object> articles = new ArrayList<Object>();

Article a = new Article();

articles.add(a);

String str = JsonUtil.getJsonStrFromList(articles);

String json = "{"touser":""+openId+"","msgtype":"news","news":" +

"{"articles":" +str +"}"+"}";

json = json.replace("picUrl", "picurl");

System.out.println(json);

//获取access_token

String access_token = mediaUtil.getAccess_token();

//获取请求路径

try {

connectWeiXinInterface(action,json);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* @param action 接口url

* @param json 请求接口传送的json字符串

*/

public void connectWeiXinInterface(String action,String json){

URL url;

try {

url = new URL(action);

HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setRequestMethod("POST");

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();

OutputStream os = http.getOutputStream();

os.write(json.getBytes("UTF-8"));// 传入参数

InputStream is = http.getInputStream();

int size = is.available();

byte[] jsonBytes = new byte[size];

is.read(jsonBytes);

String result = new String(jsonBytes, "UTF-8");

System.out.println("请求返回结果:"+result);

os.flush();

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

转载地址:http://jingyan.baidu.com/article/b907e627b1e4bb46e7891cf0.html

欢迎访问我的技术群425783133

经验分享 程序员 微信小程序 职场和发展