博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
缩短网站地址
阅读量:6539 次
发布时间:2019-06-24

本文共 9066 字,大约阅读时间需要 30 分钟。

hot3.png

http://www.alifeifei.net/

 

http://blog.sina.com.cn/s/blog_716b5a990102v1mi.html

http://blog.sina.com.cn/s/blog_15f27f8cf0102wno5.html

http://open.weibo.com/wiki/Short_url/shorten

 

 

public static void main(String[] args) {		Map
hashMap = 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();      }  }

 

转载于:https://my.oschina.net/yizhichao/blog/892226

你可能感兴趣的文章
Node.js + Express 4.x + MongoDB 构建登录注册(二)
查看>>
关于十六进制和八进制负数的问题
查看>>
连接池并发的实现原理
查看>>
Range
查看>>
爬虫之lxml - etree - xpath的使用
查看>>
PyalgoTrade 打印收盘价(二)
查看>>
关于C语言指针【第二季】
查看>>
MYSQLi数据访问批量删除
查看>>
浪潮K-UNIX操作系统了解
查看>>
less: CSS 预处理语言
查看>>
知识管理系统VS文档管理系统的区别【转】
查看>>
最近点对
查看>>
《团队作业第三、第四周》五小福团队作业--Scrum 冲刺阶段--Day2
查看>>
PHP为什么会被认为是草根语言?
查看>>
解决NetBeans编辑器中文乱码问题
查看>>
ztree-demo 2
查看>>
javascript常用方法
查看>>
decode行转列,case when,
查看>>
C#数据类型与数据库字段类型对应
查看>>
JAVA 查找类的所有引用关系(python实现)
查看>>