首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

短因特网址应用和实现

2013-08-09 
短网址应用和实现? ? ? ?随着移动技术的发展,手机短信接手网址的时候,短网址成为流行,字数少,省流量。? ? ?

短网址应用和实现

? ? ? ?随着移动技术的发展,手机短信接手网址的时候,短网址成为流行,字数少,省流量。

? ? ? ?以下是我采用java调用新浪的短网址服务器实现的短网址转换。

? ? ? ?短网址转换服务是直接采用新浪的: ? ? http://open.weibo.com/wiki/2/short_url/shorten

?

? ? ? 代码实现如下:

? ? ? ? ??//需要转换的原始

String website = "www.hao123.com";

try {

? ? ? ?if (StringUtils.isNotBlank(website)) {

? ? ? ? ? ?if(website.indexOf("http://")==-1){

? ? ? ? ? ? ? ?website = "http://"+website;

? ? ? ? ? ?}

? ? ? ? ? ?Map<String, String> param = new HashMap<String, String>();

? ? ? ? ? ?param.put("source", "3297620043");

? ? ? ? ? ?param.put("url_long", website);

? ? ? ? ? ?

? ? ? ? ? ?String postUrl = "http://api.weibo.com/2/short_url/shorten.json";

? ? ? ? ? ?HttpClient client = new HttpClient();

? ? ? ? ? ?client.getParams().setParameter("http.protocol.content-charset", "UTF-8");

? ? ? ? ? ?client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

? ? ? ? ? ?client.getHttpConnectionManager().getParams().setSoTimeout(5000);

? ? ? ? ? ?

? ? ? ? ? ?PostMethod method = new PostMethod(postUrl);

? ? ? ? ? ?if(!param.isEmpty()){

? ? ? ? ? ? ? ?Set<String> set = param.keySet();

? ? ? ? ? ? ? ?Iterator<String> it = set.iterator();

? ? ? ? ? ? ? ?while(it.hasNext()){

? ? ? ? ? ? ? ? ? ?String key = it.next();

? ? ? ? ? ? ? ? ? ?method.setParameter(key, param.get(key)); ? ?

? ? ? ? ? ? ? ?}

? ? ? ? ? ?}

? ? ? ? ? ?

? ? ? ? ? ?client.executeMethod(method);

? ? ? ? ? ?String response = method.getResponseBodyAsString();

? ? ? ? ? ?

? ? ? ? ? ? ? ? JSONObject result = new JSONObject(response);

? ? ? ? ? ? ? ? if (result != null) {

? ? ? ? ? ? ? ? ? ? JSONArray jarry = (JSONArray) result.get("urls");

? ? ? ? ? ? ? ? ? ? if(jarry !=null && !jarry.isNull(0)){

? ? ? ? ? ? ? ? ? ? ? ? JSONObject json = (JSONObject) jarry.get(0);

? ? ? ? ? ? ? ? ? ? ? ? String tinyurl = json.get("url_short").toString();

? ? ? ? ? ? ? ? ? ? ? ? System.out.println(tinyurl);

? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ?}

} catch (Exception e) {

? ? ? ? ? ? // TODO Auto-generated catch block

? ? ? ? }

?

? ? ? ?运行就直接可以得到www.hao123.com的短网址(http://t.cn/hizks)

?

热点排行