调用新浪网址缩短服务生成网址短链接

版权声明:本文为「简简单单 Online zuozuo」原创文章,非商业用途欢迎转载,请保持署名,注明出处! Java 交流QQ 群:172083832 ,欢迎大家加入! https://blog.csdn.net/qq_15071263/article/details/78282944

调用新浪网址缩短服务生成网址短链接

// 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;
    }

}
展开阅读全文

没有更多推荐了,返回首页