java代码如下
/**
* 发送注册成功模板消息
* @return
*/
public static String toSendModelMessage(String openId,String phone,String id,String dayStr){
String accessToken=getAccessToken();
if(accessToken.length()<=0){
return "";
}
System.out.println("accessToken==="+accessToken);
String urlStr="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.connect();
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
String params="{"+
"\"touser\":\""+openId+"\","+
"\"template_id\":\"ZvG8sGRKqo2Hk-OFRkHVLbvaeZTX7DDJFyS3G-0mb4A\","+
"\"url\":\"http://map.baidu.com/\","+
"\"topcolor\":\"#FF0000\","+
"\"data\":{"+
"\"first\": {"+
"\"value\":\"客官,恭喜您成功注册为VIP\","+
"\"color\":\"#173177\""+
"},"+
"\"keyword1\":{"+
"\"value\":\""+phone+"\","+
"\"color\":\"#173177\""+
"},"+
"\"keyword2\":{"+
"\"value\":\""+id+"\","+
"\"color\":\"#173177\""+
"},"+
"\"keyword3\":{"+
"\"value\":\""+dayStr+"\","+
"\"color\":\"#173177\""+
"},"+
"\"remark\":{"+
"\"value\":\"点击跳转\","+
"\"color\":\"#173177\""+
"}"+
"}"+
"}";
System.out.println(params);
out.writeBytes(params);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
String line;
String responseStr="";
while ((line = reader.readLine()) != null){
responseStr+=line;
}
System.out.println(responseStr);
reader.close();
urlConnection.disconnect();
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
返回的结果如下
{"errcode":47001,"errmsg":"data format error hint: [IGparA0715ge14]"}
以下是我打印出来的请求json
{
"touser":"on3mAwuljdY12JuRbua28EUCgW9o",
"template_id":"ZvG8sGRKqo2Hk-OFRkHVLbvaeZTX7DDJFyS3G-0mb4A",
"url":"http://map.baidu.com/",
"topcolor":"#FF0000",
"data":{
"first":{
"value":"客官,恭喜您成功注册为VIP",
"color":"#173177"
},
"keyword1":{
"value":"18520040243",
"color":"#173177"
},
"keyword2":{
"value":"5",
"color":"#173177"
},
"keyword3":{
"value":"2014年7月21日 18:36",
"color":"#173177"
},
"remark":{
"value":"点击跳转",
"color":"#173177"
}
}
}
很想知道我错在哪!求有经验的同学指点下