http://www.alifeifei.net/
http://blog.sina.com.cn/s/blog_716b5a990102v1mi.html
http://blog.sina.com.cn/s/blog_15f27f8cf0102wno5.htmlhttp://open.weibo.com/wiki/Short_url/shorten
public static void main(String[] args) { MaphashMap = new HashMap<>(); String url = "http://appdownload.allcam.com.cn/"; hashMap.put("source", "3271760578"); hashMap.put("url_long",url.toString()); String urlJson = httpGetRequest("http://api.t.sina.com.cn/short_url/shorten.json",hashMap); System.out.println(urlJson); }
恰好碰到这个问题
[{"url_short":"http://t.cn/Raw9LVX","url_long":"http://appdownload.allcam.com.cn/","type":0}][2017-05-04 20:44:42,918][ERROR][com.allcam.andedu.abc.utils.JsonParseUtil.json2Obj(JsonParseUtil.java:97)][main] - JsonMappingException:Json Convert Object Error... - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.allcam.andedu.abc.utils.UrlShortResp out of START_ARRAY token at [Source: [{"url_short":"http://t.cn/Raw9LVX","url_long":"http://appdownload.allcam.com.cn/","type":0}]; line: 1, column: 1] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835) at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1229) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:165) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2578) at com.allcam.andedu.abc.utils.JsonParseUtil.json2Obj(JsonParseUtil.java:89) at com.allcam.andedu.abc.utils.ShortUrlUtils.main(ShortUrlUtils.java:14)null
修改代码 public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { String url = "http://appdownload.allcam.com.cn/"; String urlJson = sendGet("http://api.t.sina.com.cn/short_url/shorten.json", "source=3271760578&url_long=" + url.toString()); System.out.println(urlJson);// UrlShortResp resp = JsonParseUtil.json2Obj(urlJson, UrlShortResp.class); ObjectMapper mapper = new ObjectMapper(); UrlShortResp resp = mapper.readValue(urlJson, UrlShortResp[].class)[0]; System.out.println(resp); }
工具类
package com.allcam.andedu.abc.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;public class ShortUrlUtils { public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { String url = "http://appdownload.allcam.com.cn/"; String urlJson = sendGet("http://api.t.sina.com.cn/short_url/shorten.json", "source=3271760578&url_long=" + url.toString()); System.out.println(urlJson);// UrlShortResp resp = JsonParseUtil.json2Obj(urlJson, UrlShortResp.class); ObjectMapper mapper = new ObjectMapper(); UrlShortResp resp = mapper.readValue(urlJson, UrlShortResp[].class)[0]; System.out.println(resp); } public static String getShortUrl(String url) { UrlShortResp resp = null; try { String urlJson = sendGet("http://api.t.sina.com.cn/short_url/shorten.json", "source=3271760578&url_long=" + url.toString()); System.out.println(urlJson); ObjectMapper mapper = new ObjectMapper(); resp = mapper.readValue(urlJson, UrlShortResp[].class)[0]; System.out.println(resp); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return resp.getUrl_short(); } /** * 向指定URL发送GET方法的请求 * @param url 发送请求的URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ /** * @author 向蓬 * @date 2016-5-20上午10:31:11 * @param url * @param param * @return * String */ public static String sendGet(String url, String param) { StringBuffer result = null; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; result = new StringBuffer(); while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { } finally { //释放资源 try { if (in != null) { in.close(); } } catch (Exception e) { } } return result.toString(); } // /** // * URLConnection发送get请求 // * @author 向蓬 // * @date 2016-5-20上午10:39:25 // * @param url // * @param param // * @return // * String // */ // public static String httpGetRequest(String url, String param) { // StringBuffer result = null; // BufferedReader in = null; // try { // String urlNameString = url + "?" + param; // System.out.println("请求url:"+urlNameString); // URL realUrl = new URL(urlNameString); // // 打开和URL之间的连接 // URLConnection connection = realUrl.openConnection(); // // 设置通用的请求属性 // connection.setRequestProperty("accept", "application/json;charset=UTF-8"); // connection.setRequestProperty("connection", "Keep-Alive"); // connection.setRequestProperty("user-agent", // "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // // 建立实际的连接 // connection.connect(); // // 定义 BufferedReader输入流来读取URL的响应 // in = new BufferedReader(new InputStreamReader( // connection.getInputStream())); // String line; // result = new StringBuffer(); // while ((line = in.readLine()) != null) { // result.append(line); // } // } catch (Exception e) { // } finally { // //释放资源 // try { // if (in != null) { // in.close(); // } // } catch (Exception e) { // } // } // return result.toString(); // } /** * URLConnection发送get请求 * @author 向蓬 * @date 2016-5-20上午10:39:25 * @param url * @param param * @return * String */ public static String httpGetRequest(String url, String param) { StringBuffer result = null; BufferedReader in = null; try { String urlNameString = url + "?" + param; System.out.println("请求url:"+urlNameString); URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "application/json;charset=UTF-8"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; result = new StringBuffer(); while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { } finally { //释放资源 try { if (in != null) { in.close(); } } catch (Exception e) { } } return result.toString(); } }