android:AndroidAnnotations上傳檔案,網路介面如此簡潔

來源:互聯網
上載者:User

android:AndroidAnnotations上傳檔案,網路介面如此簡潔
網路介面如此簡潔

使用HttpClient進行檔案的上傳,可以參考部落格:使用HttpClient進行檔案上傳
如果項目使用AndroidAnnotation,寫上傳介面就會非常方便,比如之前寫POST介面
首先參考之前的部落格,使用AndroidAnnotations進行POST請求。

如下是使用AndroidAnnotations進行檔案上傳的網路介面
@Rest(rootUrl = "http://192.168.31.183:8080/SSHMySql/", converters = {GsonHttpMessageConverter.class})public interface FileService extends RestClientErrorHandling{    @Post("/upload.action")    @Accept(MediaType.APPLICATION_JSON)    ReqResult uploadFile(MultiValueMap params);}

調用時傳遞參數,其中”userImage”欄位為伺服器端接收檔案的欄位,desc為另外一個參數,當然,你也可以添加額外的參數。

如下為調用網路介面時的代碼
public ReqResult postUploadFile(String localFilePath,String desc) {        try {            if (!new File(localFilePath).exists()) {                return null;            }            MultiValueMap params = new LinkedMultiValueMap();            params.add("userImage", new FileSystemResource(localFilePath));            params.add("desc",desc);            return fileService.uploadFile(params);        } catch (Exception e) {            e.printStackTrace();        }        return null;    }

這裡最需要說明的是“userImage”參數的值為:new FileSystemResource(localFilePath)

Android開發聯盟QQ群:272209595

未經許可,不得用於商業目的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.