這幾天很有興致的學習了百度雲盤文件API接口的使用;初步是想做一個在線android應用,應用中的文檔是存放在百度雲盤的。
主要是分一下幾個步驟:
1.註冊百度賬號
2.登錄百度開發者中心
3.創建移動應用,獲取對應的(API Key Secret Key)
4.開通pcs API權限
6.開發應用
注意:
開通移動應用,獲取key
獲取token的時候我使用的安卓獲取的方式
通過我寫對應api的例子我發現,其實就兩種情況:一種是get方式提交數據,另外一種是post方式提交數據
1.get方式提交數據,我們用獲取雲盤的信息爲例:
獲取雲盤信息前我們要知道,我們要準備好什麼數據:
請求參數:
url: 標明我們要訪問的網址路徑 值固定問“https://pcs.baidu.com/rest/2.0/pcs/quota”
method:標明我們是請求雲盤信息 值固定爲“info”
acceess_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 sb = null;
try {
isr = new InputStreamReader(conn.getInputStream(),"gb2312");
br = new BufferedReader(isr);
String line = null;
sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.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 sb;
}
/**
* @return
* @throws Exception
* 獲取雲空間的信息
*/
public CloudInfo getCloudInfo() throws Exception {
URL u = new URL(https://pcs.baidu.com/rest/2.0/pcs/quota"?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 com.entity;
import java.lang.reflect.Type;
/**
* @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) {
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()/1024/1024).append("M; 已用:").append(this.getused()/1024/1024).append("M; ").toString();
}
}
2.通過post方式提交 我用上傳單個文件爲例子:
同樣我們也先了解下上傳文件要參數設置:
請求參數:
url: 標明我們要訪問的網址路徑 值固定問“https://pcs.baidu.com/rest/2.0/pcs/file”
method:標明我們是請求雲盤信息 值固定爲“upload”
acceess_token:准入標識 值是我們自己申請的
path:是我們要上傳到雲盤的那個路徑下 如/apps/myBaiduCloud/ myBaiduCloud是我們的應用名稱(當你獲取koten後就會自動生成以你應用名稱爲名的文件夾)
file:這個就是我們要上傳的文件了(要求用post方式上傳)
ondup:可選參數,標識當有重名的文件的時候處理方式具體見api
接收返回參數:
返回的也是json串,
path:爲我們上傳的文件保存的全路徑
size:文件的大小有多少字節
ctime/mtime:文件的創建修改時間
其他參數介紹點小標題去api中查看
{
"path" : "/apps/album/README.md"
"size" : 372121,
"ctime" : 1234567890,
"mtime" : 1234567890,
"md5" : "cb123afcc12453543ef",
"fs_id" : 12345,
"request_id":4043312669
}
/**
* @param path 雲盤存放路徑
* @param name 要上傳的文件
* @return
* @throws Exception
*/
public FileBase uploadFile(String path,File file) throws Exception{
//模擬文件
String fileName="README.md";
file = new File(fileName);
path="%2fapps%2fmybaidu%2f"; // 我用的是url編碼過源碼爲:-> "/apps/mybaidu/
/"
//將需要url傳值的參數和url組裝起來
String u ="https://pcs.baidu.com/rest/2.0/pcs/file?path="+path+file.getName()+"&method=upload&access_token=你自己申請的token值";
PostMethod filePost = new PostMethod(u);
//post提交的參數
Part[] parts = {new FilePart(fileName,file)};
//設置多媒體參數,作用類似form表單中的enctype="multipart/form-data"
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient clients = new HttpClient();
//響應代碼
int status = clients.executeMethod(filePost);
System.out.println("成功上傳"+path+fileName);
BufferedReader buReader = new BufferedReader(new InputStreamReader(filePost.getResponseBodyAsStream(),"utf-8"));
StringBuffer sb = new StringBuffer();
String line;
while((line=buReader.readLine())!=null){
sb.append(line);
}
buReader.close();
// 解析成對象
Gson gson = new Gson();
FileBase cloudInfo = gson.fromJson(sb.toString(), FileBase.class);
return cloudInfo;
}
上面代碼成功後我們就會在/apps/mybaidu/目錄下找到README.md文件
上面代碼執行還要倒入對應的jar包:下載
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging.jar
gson-2.2.1.jar
jsoup-1.6.3.jar
其他的api怎麼用百度給了一個很好的演示平臺:
好了剩下的就大家自己去模式吧!
用什麼問題聯繫我我們一起探討----ydcun