- content_work: 文本内容类网络请求
- file_work: 文件类网络请求
- ALiOSSFileServer: AliOSS文件服务
1. OKHttpManager 使用
OKHttpManager manager = new OKHttpManager(
new RequestTimeInterceptor() {
public void requestTIme(String requestUrl, int requestTime) {
Log.e(TAG, "" + requestUrl + " : " + requestTime);
}
},
new TokenInterceptor() {
public String getToken() {
return "66666666";
}
}
);
// sync post
String data = manager.post(url, "666", PostType.JSON);
// async post
manager.post(url, "666", PostType.JSON, new IContentCallback() {
public void onSuccess(int code, String data) {
Log.e(TAG, "" + code + " : " + data);
}
public void onError(int code, String error) {
Log.e(TAG, "" + code + " : " + error);
}
});
2. ALiOSSFileServer 使用
private static final String FILE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
public void onclick(View view) {
String STSServer = "https://192.168.10.124:7080/getAliToekn";
String EndPoint = "cj-oss.save.xyz";
String BucketName = "databucket";
String prefix = "666/";
ALiOSSFileServer aLiOSSFileServer = new ALiOSSFileServer(this, STSServer, EndPoint, BucketName, prefix){
@Override
public OSSFederationToken getTokenFromUser(String STSServer) {
try {
// Get temporary token.
OKHttpManager manager = new OKHttpManager(new TokenInterceptor() {
@Override
public String getToken() {
return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzE2MyIsImlhdCI6MTU4OTYxNDczNSwiZXhwIjoxNTkwMjE5NTM1fQ.DyuUllLfRAmv85OY2e9pjME1jQ2o44tsSu4cr4iRVwYljg6y9gml_f3h0ztEsrtBAw_y3ztk3QhjzuiIpAIUmw";
}
});
String message = manager.get(STSServer);
Gson gson = new Gson();
STSBean bean = gson.fromJson(message, STSBean.class);
Log.e(TAG, "" + bean);
if(bean.code != 200){
return null;
}
STSBean.DataBean dataBean = bean.data;
Log.e(TAG, "" + dataBean);
return new OSSFederationToken(dataBean.AccessKeyId, dataBean.AccessKeySecret, dataBean.SecurityToken, dataBean.Expiration);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NetErrorException e) {
e.printStackTrace();
}
return null;
}
};
// Single file upload
FileBean fileBean = new FileBean();
fileBean.setPath2AliOSS(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
aLiOSSFileServer.uploadFileByAliOSS(fileBean, new IProgress() {
@Override
public void onProgress(int progress, long currentSize, long totalSize) {
Log.e(TAG, "" + progress + " : " + currentSize + " : " + totalSize);
}
}, new FileWorkCallbackImpl() {
@Override
public void onSuccess(FileBean fileBean) {
Log.e(TAG, "" + fileBean);
}
@Override
public void onError(String errorMessage) {
Log.e(TAG, "" + errorMessage);
}
});
// Multi-file upload
List<FileBean> files = new ArrayList<>();
for (int i = 1; i < 10; i++) {
FileBean fileBean2 = new FileBean();
fileBean2.setPath2AliOSS(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
files.add(fileBean2);
}
aLiOSSFileServer.uploadFilesByAliOSS(files, new IProgress() {
@Override
public void onProgress(int progress, long currentSize, long totalSize) {
Log.e(TAG, "" + progress);
}
}, new FileWorkCallbackImpl(){
@Override
public void onSuccess(List<FileBean> networkUrls) {
for (FileBean fileBean : networkUrls) {
Log.e(TAG, "" + fileBean.getPath2AliOSS());
}
}
@Override
public void onError(String errorMessage) {
Log.e(TAG, "" + errorMessage);
}
});
}