ShorturlApi.java
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Copyright (c) 2011-2015, Unas 小强哥 (unas@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.jfinal.weixin.sdk.api;
import java.util.HashMap;
import java.util.Map;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
/**
* 将一条长链接转成短链接 API
* 文档地址:http://mp.weixin.qq.com/wiki/6/d2ec191ffdf5a596238385f75f95ecbe.html
*/
public class ShorturlApi
{
private static String apiUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=";
public static ApiResult getShorturl(String jsonStr) {
String jsonResult = HttpUtils.post(apiUrl + AccessTokenApi.getAccessTokenStr(), jsonStr);
return new ApiResult(jsonResult);
}
/**
* 长链接转短链接接口
* @param longUrl 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url
* @return ApiResult 短连接信息
*/
public static ApiResult getShortUrl(String longUrl) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("action", "long2short");
params.put("long_url", longUrl);
return getShorturl(JsonUtils.toJson(params));
}
}