BufferedReader reader = null;
try {
longurl = URLEncoder.encode(longurl, "GBK");
URL url = new URL("http://api.t.sina.com.cn/short_url/shorten.json?source=2546260130&url_long=" + longurl);
InputStream iStream = url.openStream();
reader = new BufferedReader(new InputStreamReader(iStream));
String jsonStr = reader.readLine();
JSONObject jsonObj = JSONObject.parseArray(jsonStr).getJSONObject(0);
return jsonObj.getString("url_short");
} catch (Exception e) {
return longurl;
} finally {
IOUtils.closeQuietly(reader);
}
转载于:https://my.oschina.net/wangchongya/blog/708651