新浪短网址接口调用代码,用的自取哦!

  以下是suo.nz短网址为您找的新浪短网址(t.cn)的接口调用代码,有用的自取一下哈!

  // POJO类

  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;

  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;

  }

  }