收藏
回答

在Ios上推送模板消息没事,在Android上就报400

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 模板消息 工具 v1.02.1808101 2.2.3


        @RequestMapping("/template_type.do")

@ResponseBody

public static ResponseResult<Void> makeRouteMessage(Template template, String token, String[] array){

System.out.println(Arrays.toString(array));

System.out.println();

Map<String, Map<String, Object>> data = new LinkedHashMap<String, Map<String,Object>>();

Map<String, Object> map1 = new HashMap<String, Object>();

map1.put("value", array[0]);

data.put("keyword1", map1);

Map<String, Object> map2 = new HashMap<String, Object>();

map2.put("value", array[1]);

data.put("keyword2", map2);

Map<String, Object> map3 = new HashMap<String, Object>();

map3.put("value", array[2]);

data.put("keyword3", map3);

template.setData(data);

net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(template);

System.out.println(template);

return sendTemplateMessage(token, jsonObject+"");

}

public static JSONObject httpRequest(String requestUrl , String requestMethod , String outputStr) {

        JSONObject jsonObject = null;

        StringBuffer buffer = new StringBuffer();

        try {

            // 创建SSLContext对象,并使用我们指定的信任管理器初始�?

            TrustManager[] tm = { new MyX509TrustManager() };

            SSLContext sslContext = SSLContext.getInstance("SSL" , "SunJSSE");

            sslContext.init(null , tm , new java.security.SecureRandom());

            // 从上述SSLContext对象中得到SSLSocketFactory对象

            SSLSocketFactory ssf = sslContext.getSocketFactory();


            URL url = new URL(requestUrl);

            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();

            httpUrlConn.setSSLSocketFactory(ssf);


            httpUrlConn.setDoOutput(true);

            httpUrlConn.setDoInput(true);

            httpUrlConn.setUseCaches(false);

            // 设置请求方式(GET/POST�?

            httpUrlConn.setRequestMethod(requestMethod);


            if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect();


            // 当有数据需要提交时

            if (null != outputStr) {

                OutputStream outputStream = httpUrlConn.getOutputStream();

                // 注意编码格式,防止中文乱�?

                outputStream.write(outputStr.getBytes("UTF-8"));

                outputStream.close();

            }


            // 将返回的输入流转换成字符�?

            InputStream inputStream = httpUrlConn.getInputStream();

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream , "utf-8");

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


            String str = null;

            while ((str = bufferedReader.readLine()) != null) {

                buffer.append(str);

            }

            bufferedReader.close();

            inputStreamReader.close();

            // 释放资源

            inputStream.close();

            inputStream = null;

            httpUrlConn.disconnect();

//          jsonObject = JSONObject.fromObject(buffer.toString());

            return JSONObject.parseObject(buffer.toString());

        } catch (ConnectException ce) {

            System.out.println("Weixin server connection timed out.");

        } catch (Exception e) {

            System.out.println("error.");

        }

      return jsonObject;

    }

/**

* 发送消息

* @param accessToken

* @param jsonMsg

* @return

*/

public static ResponseResult<Void> sendTemplateMessage(String accessToken, String jsonMsg){

System.out.println("skfasdhfkdsa: " + jsonMsg);

ResponseResult<Void> rr = null;

//请求地址

String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN";

requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);

//发送模板消息

JSONObject jsonObject = httpRequest(requestUrl, "POST", jsonMsg);

if(null != jsonObject){

int errorCode = jsonObject.getIntValue("errcode");

String errorMsg = jsonObject.getString("errmsg");

if(0 == errorCode){

rr = new ResponseResult<Void>(errorCode, errorMsg);

System.out.println("模板消息发送成功errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");

}else{

System.out.println("模板消息发送失败errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");

rr = new ResponseResult<Void>(errorCode, errorMsg);

}

}

return rr;

}


回答关注问题邀请回答
收藏

1 个回答

  • 小吉祥天
    小吉祥天
    2018-09-29

    现在在远程调试中测试  然后就是死活不进  /template_type.do

    2018-09-29
    有用
    回复
登录 后发表内容