调用新浪网址缩短服务生成网址短链接
发布时间
阅读量:
阅读量
调用新浪网址缩短服务生成网址短链接
// POJO类
/** * Created by 谭健 on 2017/10/17. 13:59.
* © All Rights Reserved.
*/
public class XinlangDwzResult {
private String request;
private Integer error_code;
private String error;
private String url_short;
private String url_long;
private Integer type;
private boolean result;
@Override
public String toString() {
return "XinlangDwzResult{" +
"request='" + request + '\'' +
", error_code=" + error_code +
", error='" + error + '\'' +
", url_short='" + url_short + '\'' +
", url_long='" + url_long + '\'' +
", type=" + type +
", result=" + result +
'}';
}
public String getRequest() {
return request;
}
public void setRequest(String request) {
this.request = request;
}
public Integer getError_code() {
return error_code;
}
public void setError_code(Integer error_code) {
this.error_code = error_code;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getUrl_short() {
return url_short;
}
public void setUrl_short(String url_short) {
this.url_short = url_short;
}
public String getUrl_long() {
return url_long;
}
public void setUrl_long(String url_long) {
this.url_long = url_long;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
}
代码解读
// 工具类
import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.NameValuePair;
/** * Created by 谭健 on 2017/10/17. 13:14.
* © All Rights Reserved.
*/
public class ShortUrlUtil {
private static final String POST_URL = "http://api.t.sina.com.cn/short_url/shorten.json";
private static final String appKey = "你的appKey";
public static XinlangDwzResult generateShortUrl(String url) {
NameValuePair[] pairs = {new NameValuePair("source", appKey), new NameValuePair("url_long", url)};
// 怎么Post就不说了
String postResult = Post.run(POST_URL, pairs);
// 返回一个Json,用什么Json工具都行,无所谓
// subString是因为他会返回一个数组,因为支持多网址调用,这里用一个就好了
XinlangDwzResult resutl = JSON.parseObject(postResult.substring(1, postResult.length() - 1), XinlangDwzResult.class);
System.out.println(resutl.toString());
return resutl;
}
}
代码解读
全部评论 (0)
还没有任何评论哟~
