上傳文件的核心功能主要是在UploadCommand.java中實(shí)現(xiàn)
使用步驟:
1 創(chuàng)建UploadCommand的構(gòu)建類Builder
UploadCommand.Builder builder = new UploadCommand.Builder();
2 通過構(gòu)建類設(shè)置UploadCommand的各種屬性
builder.domain("上傳地址的域名") .path("上傳接口") .contentType("請求頭文件的Content-type") .mediaType(Params.Body.MEDIA_TYPE_VIDEO) .file("上傳文件的絕對路徑") .transferListener(new SimpleRequestBody.ProgressListener() { // 上傳進(jìn)度的監(jiān)聽器 @Override public void onDataTransferred(long transferred, long contentLength) { Log.e("TAG", "already transfer " + 100 * ((double) transferred / (double) contentLength)); } });3 通過構(gòu)建類構(gòu)造UploadCommand對象,并啟動
UploadCommand uploadCommand = builder.build();uploadCommand.start(this, new AppResultReceiver(new Handler(), MainActivity.this));
注意:以上代碼需要將MainActivity實(shí)現(xiàn)AppResultReceiver.ResultListener接口
4 在onResultSuccess方法中,區(qū)分是否是UploadCommand,并解析數(shù)據(jù)
@Override public void onResultSuccess(Bundle resultData) { if (null != resultData) { if (UploadCommand.UPLOAD_CMD.equals( resultData.getString(Params.CommandMessage.CMD_CODE))) { Logger.e("JIANG", "上傳成功"); } } }框架github地址: SimpleCommand框架
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選