优酷接口的使用

2、1定义优酷中的应用id

//应用Id
var client_id = “3f55944758a967be”;
var access_token = “feb81d9c7eff2b440d302770a4f389d5”;

2.2 获取视频状态

/**
* 加载视频状态
*/
function loadVideoStatus() {
//判断视频的状态
if(videoStatus!=-1&&videoStatus!=3&&videoStatus!=2){
var jobSooId = $(“#jobSoo_id_hidden”).val();
$.ajax({
// 请求URl
url :  “jobSoo/updateVideoStatus”,
// 请求参数
data : {
“client_id” : client_id,
“video_id” : videoId,
“jobSooId”:jobSooId
},
// 数据类型
dataType : ‘json’,
// 请求方式
type : ‘POST’,
// 成功回调函数
success : function(data) {
//清空
$(“#shipin_show”).html(“”);
$(“#shipin_button”).html(“”);
var html = “”;//图片
var buttonHtml =””;//按钮
//审核中
if(data==1){
html = “<img  src=’images/shipin_shenhe.gif’ />”;
buttonHtml = “<input id=’video_jobSoo_delete’ type=’button’ value=’删除’ onclick=’deleteVideo()’ class=’Shielding_Enterprises_save’ />”;
}//已屏蔽
else if(data==2){
html = “<img  src=’images/shipin_pingbi.gif’ />”;
buttonHtml = “<input id=’video_jobSoo_delete’ type=’button’ value=’删除’ onclick=’deleteVideo()’ class=’Shielding_Enterprises_save’ />”;
}//正常
else if(data==3){
html = “<img id=’youku_loading’ src=’images/youku_loading.gif’ width=’344′ height=’205’/><div id=’youkuplayer’ style=’display:none;margin: 10px 200px; width: 300px; height: 270px’></div>”;
buttonHtml = “<input id=’video_jobSoo_delete’ type=’button’ value=’删除’ onclick=’deleteVideo()’ class=’Shielding_Enterprises_save’ style=’display:none;’ />”;
loadScript(“http://player.youku.com/jsapi”,playVideo);
}//转码中
else if(data==0){
html = “<img  src=’images/shipin_zhuanma.gif’ />”;
}
$(“#shipin_show”).html(html);
$(“#shipin_button”).html(buttonHtml);
},
error : function(xhr) {
// 跳转错误页面
window.location.href = basePath + “index.jsp”;
}
});
}
}

2.2删除视频

/**
* 删除视频
*/
function deleteVideo() {
if (window.confirm(“你确定要删除该视频内容吗?一旦删除将不可恢复”)) {
//获取求职秀Id
var jobSooId = $(“#jobSoo_id_hidden”).val();
$.ajax({
url : path + “jobSoo/deleteVideo”,
data : {
“client_id” : client_id,
“access_token” : access_token,
“video_id” : videoId,
“jobSooId” : jobSooId
},
dataType : ‘json’,
type : ‘POST’,
success : function(data) {
if(data){
$(“#shipin_show”).html(“”);
$(“#shipin_button”).html(“”);
var html = “<img id=’video_img’  src=’images/shipin_01.gif’ />”;
var buttonHtml = “<input id=’video_jobSoo’  type=’button’ value=’上传’ onclick=’toUploadVideo()’ class=’Shielding_Enterprises_save’ />”+
“<input id=’makeVideo_btn’  type=’button’ value=’录制’ class=’Shielding_Enterprises_save’ onclick=’toMakeVideo()’ />”;
$(“#shipin_show”).html(html);
$(“#shipin_button”).html(buttonHtml);

}
}
});
}
}
3、util代码

package com.tarena.hr.util;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tarena.hr.dao.ResumeDao;
import com.tarena.hr.entity.Resume;
import com.tarena.hr.entity.noTable.YouKuJsonBean;

/**
* 读取优酷平台数据的工具类
* @author 达内–教学支持与督导部–软件研发团队
* @version v1.0 2014-07-07
*
*/
public class YouKuUtil {
private static ResumeDao resumeDao;//求职秀Dao
static{
ApplicationContext  app = new ClassPathXmlApplicationContext(“applicationContext.xml”);
resumeDao = (ResumeDao)app.getBean(“resumeDao”);
}
/**
* 读取优酷提供的查询视频状态的json内容
* @param client_id 应用Key
* @param video_id 视频ID
* @return json对象
*/
public static Object getVideoStatus(String client_id,String video_id){
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建 GET方法的实例
GetMethod getMethod = new GetMethod(“https://openapi.youku.com/v2/videos/show_basic.json?client_id=”+client_id+”&video_id=”+video_id+”&random=”+(new Random().nextInt(1000)));
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
return null;
}
//读取内容
byte[] responseBody = getMethod.getResponseBody();
//处理内容
return new String(responseBody,”utf-8”);
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
return null;
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
return null;
} finally {
//释放连接
getMethod.releaseConnection();
}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注