android:AndroidAnnotations上传文件,网络接口如此简洁

来源:互联网 发布:psd数据库 编辑:程序博客网 时间:2024/06/06 02:17

网络接口如此简洁

使用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<String, Object> params);}

调用时传递参数,其中”userImage”字段为服务器端接收文件的字段,desc为另外一个参数,当然,你也可以添加额外的参数。

  • 如下为调用网络接口时的代码
public ReqResult postUploadFile(String localFilePath,String desc) {        try {            if (!new File(localFilePath).exists()) {                return null;            }            MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();            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


未经许可,不得用于商业目的

1 0
原创粉丝点击