java接入微信js-sdk

关于js-sdk大家一起不陌生;但是接入的话可能有种种问题;下面我就来分享下我在使用中的各种问题


1.必须要有个公众号;并且支持相对应的接口;你必须要有台服务器;

下面是绑定域名

java接入微信js-sdk_第1张图片

注意这里;最好是把主域名和子域名全部写进去;免得发生错误



2.下面是java代码做后台处理;调用接口实现签名

package com.base.weixin;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
public class Jssdk {


	public final static String APPID = "";


	private final static String SECRET = "";


	// 1.获取access_token
	/**
	 * 
	 * 获得ACCESS_TOKEN
	 * 
	 * 
	 * 
	 * @Title: getAccess_token
	 * 
	 * @Description: 获得ACCESS_TOKEN
	 * 
	 * @param @return 设定文件
	 * 
	 * @return String 返回类型
	 * 
	 * @throws
	 * 
	 */


	public static String getAccess_token() {


		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID + "&secret=" +SECRET;
		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);
			String message = new String(jsonBytes, "UTF-8");
			Gson gson=new Gson();
			GetAccess fromJson = gson.fromJson(message,GetAccess.class);
			//System.out.println(message);
			accessToken=fromJson.getAccess_token();
			is.close();
			
		} catch (Exception e) {


			e.printStackTrace();


		}
		return accessToken;
	}
	
	// 2.获取jsapi_ticket
	/**
     * 调用微信JS接口的临时票据
     * 
     * @param access_token 接口访问凭证
     * @return
     */
    public static String getJsApiTicket(String access_token) {
    	String ticket=null;
    	String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
        String requestUrl = url.replace("ACCESS_TOKEN", access_token);
        try {
			URL urlGet = new URL(requestUrl);
			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);
			String message = new String(jsonBytes, "UTF-8");
			Gson gson=new Gson();
			GetAccess fromJson = gson.fromJson(message,GetAccess.class);
			ticket=fromJson.getTicket();
			//System.out.println(message);
			//System.out.println(ticket); 
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return ticket;
    }
    
	
	
	public static void main(String[] args) {
		Jssdk.getJsApiTicket(Jssdk.getAccess_token());
	}
}


下面是;调用并返回相对应的数据到页面让js使用 【最好是加上缓存;接口的调用有限制】

//微信分享


String url=UrlGet.getUrl(getRequest());
Map sign = Sign.sign(Jssdk.getJsApiTicket(Jssdk.getAccess_token()), url);
setAttr("sign",sign);
//微信分享end



//以下是调用的js代码


			
			




你可能感兴趣的:(java技术性文章)