如何使用百度云API介面

如何使用百度云API介面,第1张

如何使用百度云API介面 :developerbaidu/wiki/indexphptitle=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E8%B5%84%E6%BA%90%E4%B8%8B%E8%BD%BD

学习了百度云盘档案API介面的使用;初步是想做一个线上android应用,应用中的文件是存放在百度云盘的。

主要是分一下几个步骤:

1注册百度账号

2登入百度开发者中心

3建立移动应用,获取对应的(API Key Secret Key)

4开通pcs API许可权

5获取ACCESS_token(认证编码)

6开发应用

注意:

开通移动应用,获取key

获取token的时候我使用的安卓获取的方式

通过我写对应api的例子我发现,其实就两种情况:一种是get方式提交资料,另外一种是post方式提交资料

1get方式提交资料,我们用获取云盘的资讯为例:

获取云盘资讯前我们要知道,我们要准备好什么资料:

请求引数:

url: 标明我们要访问的网址路径 值固定问""

method:标明我们是请求云盘资讯 值固定为"info"

aeess_token:准入标识 值是我们自己申请的

接收返回引数:

quota:云盘总容量

used:云盘使用容量

request_id:该请求的表示,没啥用

返回的一个json串如下格式:{"quota":123794882560, "used":83573494688,"request_id":2853739529}

我在做的时候你使用Gson工具将json串转换到对应的entity类中了 程式码如下:

[] /

@param URLConnection conn通过get方式获取StringBuffer

@return

/

private StringBuffer getJsonString(URLConnection conn) {

InputStreamReader isr = null;

BufferedReader br = null;

StringBuffer = null;

try {

isr = new InputStreamReader(conngetInputStream(),"gb2312");

br = new BufferedReader(isr);

String line = null;

= new StringBuffer();

while ((line = brreadLine()) != null) {

append(line);

append("\r\n");

}

} catch (UnsupportedEncodingException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}finally{

try {

if(isr!=null)

isrclose();

} catch (IOException e) {

Systemoutprintln("流关闭是异常");

eprintStackTrace();

}

}

return ;

}

/

@return

@throws Exception

获取云空间的资讯

/

public CloudInfo getCloudInfo() throws Exception {

URL u = new URL("method=info&aess_token=你申请的token的值";

URLConnection conn = uopenConnection(); 开启网页连结

获取使用者云盘资讯

String cloudJson = thisgetJsonString(conn)。toString();

解析成物件 下面有这个实体物件的类

Gson gson = new Gson();

CloudInfo cloudInfo = gsonfromJson(cloudJson, CloudInfoclass);

Systemoutprintln("云盘资讯:"+cloudInfo);

return cloudInfo;

}

/

@param URLConnection conn通过get方式获取StringBuffer

@return

/

private StringBuffer getJsonString(URLConnection conn) {

InputStreamReader isr = null;

BufferedReader br = null;

StringBuffer = null;

try {

isr = new InputStreamReader(conngetInputStream(),"gb2312");

br = new BufferedReader(isr);

String line = null;

= new StringBuffer();

while ((line = brreadLine()) != null) {

append(line);

append("\r\n");

}

} catch (UnsupportedEncodingException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}finally{

try {

if(isr!=null)

isrclose();

} catch (IOException e) {

Systemoutprintln("流关闭是异常");

eprintStackTrace();

}

}

return ;

}

/

@return

@throws Exception

获取云空间的资讯

/

public CloudInfo getCloudInfo() throws Exception {

URL u = new URL("method=info&aess_token=你申请的token的值";

URLConnection conn = uopenConnection(); 开启网页连结

获取使用者云盘资讯

String cloudJson = thisgetJsonString(conn)。toString();

解析成物件 下面有这个实体物件的类

Gson gson = new Gson();

CloudInfo cloudInfo = gsonfromJson(cloudJson, CloudInfoclass);

Systemoutprintln("云盘资讯:"+cloudInfo);

return cloudInfo;

}

[] package entity;

import javalangreflectType;

/

@author ydcun 获取云空间的资讯 例如:

{"quota":123794882560, 空间配额,单位为位元

"used":83573494688, 已使用空间大小 单位为位元组。

"request_id":2853739529}

/

public class CloudInfo{

private Double quota;

private Double used;

private Double request_id;

/

@return the quota 空间配额,单位为位元组

/

public Double getQuota() {

return quota;

}

/

@param quota the quota to set 空间配额,单位为位元组

/

public void setQuota(Double quota) {

thisquota = quota;

}

/

@return the used 已使用空间大小 单位为位元组

/

public Double getused() {

return used;

}

/

@param used the used to set 已使用空间大小 单位为位元组

/

public void setused(Double used) {

thisused = used;

}

/

@return the request_id

/

public Double getRequest_id() {

return request_id;

}

/

@param request_id the request_id to set

/

public void setRequest_id(Double request_id) {

thisrequest_id = request_id;

}

@Override

public String toString() {

return new StringBuffer()。append("空间容量:")。append(thisgetQuota()/1024/1024)。append("M; 已用:")。append(thisgetused()/1024/1024)。append("M; ")。toString();

}

}

package entity;

import javalangreflectType;

/

@author ydcun 获取云空间的资讯 例如:

{"quota":123794882560, 空间配额,单位为位元组

"used":83573494688, 已使用空间大小 单位为位元组。

"request_id":2853739529}

/

public class CloudInfo{

private Double quota;

private Double used;

private Double request_id;

/

@return the quota 空间配额,单位为位元组

/

public Double getQuota() {

return quota;

}

/

@param quota the quota to set 空间配额,单位为位元组

/

public void setQuota(Double quota) {

thisquota = quota;

}

/

@return the used 已使用空间大小 单位为位元组

/

public Double getused() {

return used;

}

/

@param used the used to set 已使用空间大小 单位为位元组

/

public void setused(Double used) {

thisused = used;

}

/

@return the request_id

/

public Double getRequest_id() {

return request_id;

}

/

@param request_id the request_id to set

/

public void setRequest_id(Double request_id) {

thisrequest_id = request_id;

}

@Override

public String toString() {

return new StringBuffer()。append("空间容量:")。append(thisgetQuota()/1024/1024)。append("M; 已用:")。append(thisgetused()/1024/1024)。append("M; ")。toString();

}

}

2通过post方式提交 我用上传单个档案为例子:

同样我们也先了解下上传档案要引数设定:

请求引数:

url: 标明我们要访问的网址路径 值固定问""

method:标明我们是请求云盘资讯 值固定为"upload"

aeess_token:准入标识 值是我们自己申请的

path:是我们要上传到云盘的那个路径下 如/apps/myBaiduCloud/ myBaiduCloud是我们的应用名称(当你获取koten后就会自动生成以你应用名称为名的资料夹)

file:这个就是我们要上传的档案了(要求用post方式上传)

ondup:可选引数,标识当有重名的档案的时候处理方式具体见api

接收返回引数:

返回的也是json串,

path:为我们上传的档案储存的全路径

size:档案的大小有多少位元组

ctime/mtime:档案的建立修改时间

其他引数介绍点小标题去api中检视

{

"path" : "/apps/album/READMEmd"

"size" : 372121,

"ctime" : 1234567890,

"mtime" : 1234567890,

"md5" : "cb123af12453543ef",

"fs_id" : 12345,

"request_id":4043312669

}

我在做的时候也是将其封装到实体类中了,这里和上面一样不详述,我们重点看下提交档案是怎么提交的程式码如下:

[java] /

@param path 云盘存放路径

@param name 要上传的档案

@return

@throws Exception

/

public FileBase uploadFile(String path,File file) throws Exception{

模拟档案

String fileName="READMEmd";

file = new File(fileName);

path="%2fapps%2fmybaidu%2f"; 我用的是url编码过原始码为:-> "/apps/mybaidu/

/"

将需要url传值的引数和url组装起来

String u =""+path+filegetName()+"&method=upload&aess_token=你自己申请的token值";

PostMethod filePost = new PostMethod(u);

post提交的引数

Part[] parts = {new FilePart(fileName,file)};

设定多媒体引数,作用类似form表单中的enctype="multipart/form-data"

filePostsetRequestEntity(new MultipartRequestEntity(parts, filePostgetParams()));

>

本篇文章讲述使用阿里云视频视频播放sdk中的高级播放器加上SurfaceView实现,采用id+STS方法进行视频播放。

流程:用户App获取STS凭证 -> 服务端下发STS凭证 -> 用户上传视频并获取vid -> 服务端获取STS凭证 -> 将STS凭证下发给客户端 -> 完成视频播放。

请看阿里云文档=========》》》》》》 阿里云-高级播放器Android使用说明

接下来我们来看一下安卓给我们提供的手势控制类

接口

setOnTouchListener(this);

其中定义了四种状态 NONE = 0, VOLUME = 1, BRIGHTNESS = 2, FF_REW = 3;

接下来我们来看一下我们自定义的SurfaceViewOnGestureListener继承 GestureDetectorSimpleOnGestureListener主要用到了

onDown(MotionEvent e)

onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)两个方法

a在onDown的时候把状态设置NONE

b判断横向滑动的距离大于纵向滑动的距离,就把模式赋值为快进和后退

c在onScroll中进行状态赋值,根据滑动的距离,如果按下的点在屏幕的左半部分就吧状态设置为调节亮度BRIGHTNESS,如果在右半部分就是调节音量

d各种情况调用各自的接口方法

快进和后退,我们需要知道的就是我们滑动的距离如何与视频的长度关联起来。

那么咱们就可以把视频的总长度与屏幕的总长度相比,这样就能知道你手指滑动的距离占视频的多少了。

我们可以通过 l = duration / mySurfaceViewgetWidth();来获得这个比例,然后用当前的进度加上指滑动的距离占视频的长度就是要播放的视频位置

抬起点的x坐标与按下点的X坐标所得的距离,大于0是快进,小于零是后退。

系统的音量有很多,包括通话音量值,系统铃声值,音乐音量值,闹铃音量值,等等吧。

做一下笔记以备以后用到

视频播放我们用的是音乐音量值,同样的道理,我们需要把音量和高度进行关联,我们可以控件的高度闭上最大音量得出比例后就可以知道你滑动的距离占音量的多少了。

这里有个注意点就是activity是当前的这个界面,设置的是当前的界面,离开这个界面后就不管用了。

相同的道理和滑动调节音量一样也是获得屏幕的高度比上最大的亮度,然后计算滑动的距离转换成亮度是多少。(这里不多讲了)

为什么会出现黑屏,就是按Home键再点App回来后,只有声音没有的问题,因为我们用的是SurfaceView,每次点击Home键时会销毁这个SurfaceView,再回来时又会重新创建,这样我们的阿里云播放器与SurfaceView就没有绑定了,画面就没有了。

这样我们需要 给surfaceView添加mySurfaceViewgetHolder()addCallback(this);

我们在按home键的时候会走surfaceDestroyed。这样,我们就可以在这里做一个标识,让他暂停,然后再回来的时候就会走surfaceCreated,判断标识,然后进行处理就可以了。切记一定要重新让aliyunVodPlayer与SurfaceView进行关联,这样才能有画面也有声音。

由于我们的视频在阿里云的服务器上存着,访问阿里云的服务器需要临时凭证,我们通过STS来获取Token,但是这个Token是有时间限制,正好阿里云的播放器给我们提供了播放视频出错时候的回调接口,我们只需要在这里面进行重新请求Token就可以了

安全

在app开放接口api的设计中,避免不了的就是安全性问题,因为大多数接口涉及到用户的个人信息以及一些敏感的数据,所以对这些接口需要进行身份的认证,那么这就需要用户提供一些信息,比如用户名密码等,但是为了安全起见让用户暴露的明文密码次数越少越好,我们一般在web项目中,大多数采用保存的session中,然后在存一份到cookie中,来保持用户的回话有效性。但是在app提供的开放接口中,后端服务器在用户登录后如何去验证和维护用户的登陆有效性呢,以下是参考项目中设计的解决方案,其原理和大多数开放接口安全验证一样,如淘宝的开放接口token验证,微信开发平台token验证都是同理。

签名设计

对于敏感的api接口,需使用>

 BadTokenException经常出现在这样的错误中, "Unable to add window — token null is not for an application” 。一般出现这样的错误语句中

Dialog dialog = new Dialog(getApplicationContext());

或者

Dialog dialog = new Dialog(getApplication());

getApplicationContext在有些场合可以直接用来获取上下文,但是对于Dialog及其相关类型,这样使用就会报错,也就是token null。token其实就是和window对应的令牌。

正确的使用方法是

AlertDialogBuilder builder = new AlertDialogBuilder(this);

现在Android 40系统是出来了,但各大运营商自己的升级系统还没出来,建议你等相应的运营商升级系统出来再升级,否则有可能导致一些程序不兼容。我手机型号是索尼爱立信的MT15i,也再等着升级哦。

以上就是关于如何使用百度云API介面全部的内容,包括:如何使用百度云API介面、android token怎么用、安卓视频播放(阿里云视频点播播放器SDK+SurfaceView)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9296026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存