最近在开发一款类似探探一样的APP,然后我是用php写的后台接口,前端我用安卓。虽然APP的功能就一个匹配算法难一点,其他的就安卓了,然后我现在想做一个社区发表博客的功能,然后,现在的问题是如何上传多张图片和多个字段的内容。一般多个字段为 用户名、地址、发表时间、心情,图片呢,一张或多张。我安卓用的是MVP架构模式,这个模式对大家都不是很陌生,好,先上代码!
API.class


其实我完成这个功能之前,踩了很多坑,@Body不可用和@Multipart,公用,不然出现以下报错!

然后呢!还有一些小问题,@Body @PartMap() @Part 等注解的用处,大家呢,就自己去百度,可以点击官网看看
我要上传的东西有很多,我先展示数据库字段



observe表是用户的关注者的中间表,piclist是博客图片表,关联着post表和user表,然后post表呢,要关联user表,现在我展示我的PHP借口代码:

我的接口字段有四个文字字段,一个文件字段,博客分为文字博客和图片博客,我都做了判断
然后现在是我怎么把图片上传到服务器,这个是一个问题,ok,我们开始
API service类我已经鞋好了,但是我没有解释原因:
@PartMap() Map <String,RequestBody>partMap 参数:我用来存储文字字段信息
@Part List<MultipartBody.Part> file 参数:存储文件字段
如果你是上传单一文件,你把list去掉
我的Model

RetrofitUtil.class

然后是Present:

View中的方法

我们刚开始要新建Map集合,来存储文字信息,然后我自己封装了一个方法,将文字转为RequestBody,进行传输,你们可能问,为什么不用在API里面写@Part注解,String类型做参数呢!
原因是,我爱咋写就咋写,你管我
然后存储图片的集合List
List list =new ArrayList<>();
选取图片以后是不是要获取路径,然后转换为File类型,存储在RequestBody里面,然后再转为MultipartBody.Part,然后add到集合
然后new出Present对象,进行接口调用,然后暴露View接口方法,获取传输的数据,然后判断,很简单
ok,前面的坑我踩完了,你们可以放心走了!!!!
这个是APP截图

记住我,我是大二架构师!!!!!
不懂的可以加我微信:x1789780841或者QQ:1789780841 欢迎打扰!!
网友评论