1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import net.sf.json.*;
8
9
10 public class test2 {
11
12 /**
13 * @param args
14 * @throws IOException
15 */
16 public static void main(String[] args) throws IOException {
17 // TODO Auto-generated method stub
18 String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
19 //获取id后面的字符串,也就是"XMjU0MjI2NzY0.html"
20 int no = url.indexOf("id_");
21 //获取id值,也就是"XMjU0MjI2NzY0"
22 String videoId = url.substring(no+3, url.indexOf(".html"));
23 //获取视频信息数据的URL对象
24 URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
25 //从URL对象中获取输入流
26 InputStreamReader isr = new InputStreamReader(myurl.openStream());
27 //封装
28 BufferedReader br = new BufferedReader(isr);
29 //readLine获取文本
30 String urls = br.readLine();
31 //关闭流
32 br.close();
33 //获取json对象
34 JSONObject json = JSONObject.fromObject(urls);
35 //获取json数据(data内)
36 JSONArray arr = json.getJSONArray("data");
37 //获取logo的值并打印
38 System.out.println(JSONObject.fromObject(arr.get(0)).get("logo"));
39 }
40
41 }
另一种没有用json的方案,比较小白,也是本人第一次的方法。
1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.net.URL;
5
6
7
8 public class test1 {
9
10 /**
11 * @param args
12 * @throws IOException
13 */
14 public static void main(String[] args) throws IOException {
15 // TODO Auto-generated method stub
16 //设定视频地址
17 String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
18 //获取id后面的字符串,也就是"XMjU0MjI2NzY0.html"
19 int no = url.indexOf("id_");
20 //获取id值,也就是"XMjU0MjI2NzY0"
21 String videoId = url.substring(no+3, url.indexOf(".html"));
22 //获取视频信息数据的URL对象
23 URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
24 //从URL对象中获取输入流
25 InputStreamReader isr = new InputStreamReader(myurl.openStream());
26 //封装
27 BufferedReader br = new BufferedReader(isr);
28 //readLine获取文本,然后以"/"切割文本中的信息
29 String[] urls = br.readLine().split("/");
30 isr.close();
31 br.close();
32 //因为信息数据是固定格式的,所以直接第四个元素肯定是我们所需要的。
33 String target = urls[3];
34 //对上面结果继续以"切割
35 String imgid = target.substring(0,target.indexOf("""));
36 //拼凑一下,就是我们需要的地址了
37 String img = "http://g2.ykimg.com/" + imgid;
38 System.out.println(img);
39 /*
40 结果为:http://g2.ykimg.com/1100641F464D8FBF5FA3D90209C8DF96B67E2C-567E-AB53-C132-D7787FC966AB
41 */
42 }
43
44 }
……