Baidu云盘API接口的使用使用说明

如题所述

第1个回答  2022-10-02

  这几天很有兴致的学习了百度云盘文件API接口的使用 初步是想做一个在线android应用 应用中的文档是存放在百度云盘的 主要是分一下几个步骤     注册百度账号    登录百度开发者中心    创建移动应用 获取对应的(API Key Secret Key)    开通pcs API权限    获取ACCESS_token(认证编码)    开发应用 注意     开通移动应用 获取key    获取token的时候我使用的安卓获取的方式    通过我写对应api的例子我发现 其实就两种情况 一种是get方式提交数据 另外一种是post方式提交数据    get方式提交数据 我们用获取云盘的信息为例     获取云盘信息前我们要知道 我们要准备好什么数据 请求参数     url: 标明我们要访问的网址路径 值固定问     method:标明我们是请求云盘信息 值固定为 info     acceess_token:准入标识 值是我们自己申请的 接收返回参数     quota:云盘总容量    used:云盘使用容量    request_id:该请求的表示 没啥用    返回的一个json串如下格式 { quota : used : request_id : }    我在做的时候你使用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(conn getInputStream() gb )     br = new BufferedReader(isr)     String line = null;    *** = new StringBuffer()     while ((line = br readLine()) != null) {    *** append(line)     *** append( \r\n )     }    } catch (UnsupportedEncodingException e) {    e printStackTrace()     } catch (IOException e) {    e printStackTrace()     }finally{    try {    if(isr!=null)    isr close()     } catch (IOException e) {    System out println( 流关闭是异常 )     e printStackTrace()     }    }    return *** ;    }    /**    * @return    * @throws Exception    * 获取云空间的信息    */    public CloudInfo getCloudInfo() throws Exception {    URL u = new URL( ?method=info&access_token=你申请的token的值 ;    URLConnection conn = u openConnection() // 打开网页链接    // 获取用户云盘信息    String cloudJson = this getJsonString(conn) toString()

  // 解析成对象 下面有这个实体对象的类    Gson gson = new Gson()     CloudInfo cloudInfo = gson fromJson(cloudJson CloudInfo class)     System out println( 云盘信息 +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(conn getInputStream() gb )     br = new BufferedReader(isr)     String line = null;    *** = new StringBuffer()     while ((line = br readLine()) != null) {    *** append(line)     *** append( \r\n )     }    } catch (UnsupportedEncodingException e) {    e printStackTrace()     } catch (IOException e) {    e printStackTrace()     }finally{    try {    if(isr!=null)    isr close()     } catch (IOException e) {    System out println( 流关闭是异常 )     e printStackTrace()     }    }    return *** ;    }    /**    * @return    * @throws Exception    * 获取云空间的信息    */    public CloudInfo getCloudInfo() throws Exception {    URL u = new URL( ?method=info&access_token=你申请的token的值 ;    URLConnection conn = u openConnection() // 打开网页链接    // 获取用户云盘信息    String cloudJson = this getJsonString(conn) toString()     // 解析成对象 下面有这个实体对象的类    Gson gson = new Gson()     CloudInfo cloudInfo = gson fromJson(cloudJson CloudInfo class)     System out println( 云盘信息 +cloudInfo)     return cloudInfo;    }    [] package entity;    import java lang reflect Type;    /**    * @author ydcun 获取云空间的信息 例如     * { quota : 空间配额 单位为字节    * used : 已使用空间大小 单位为字节     * request_id : }    */    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) {    this quota = quota;    }    /**    * @return the used 已使用空间大小 单位为字节    */    public Double getused() {    return used;    }    /**    * @param used the used to set 已使用空间大小 单位为字节    */    public void setused(Double used) {    this used = 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) {    this request_id = request_id;    }    @Override    public String toString() {    return new StringBuffer() append( 空间容量 ) append(this getQuota()/ / ) append( M; 已用 ) append(this getused()/ / ) append( M; ) toString()     }    }    package entity;    import java lang reflect Type;    /**    * @author ydcun 获取云空间的信息 例如     * { quota : 空间配额 单位为字节    * used : 已使用空间大小 单位为字节     * request_id : }    */    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) {    this quota = quota;    }    /**    * @return the used 已使用空间大小 单位为字节    */    public Double getused() {    return used;    }    /**    * @param used the used to set 已使用空间大小 单位为字节    */    public void setused(Double used) {    this used = 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) {    this request_id = request_id;    }    @Override    public String toString() {    return new StringBuffer() append( 空间容量 ) append(this getQuota()/ / ) append( M; 已用 ) append(this getused()/ / ) append( M; ) toString()     }    }

lishixinzhi/Article/program/Java/hx/201311/27162

相似回答