【问题】当同一个接口需要同时上传多张图片,多个语音文件,还有其他普通类型的参数。得 怎处理网络层和View层代码?
【法1】
(1) 网络层:
【代码】
public void upload(Map partMap) {
TempService service = RetrofitServiceManager.getInstance().create(TempService.class);
observe(service.commitReply(partMap), new Observer() {
@Override
public void onCompleted() { // todo }
@Override
public void onError(Throwable e) { // todo }
@Override
public void onNext(BaseResponse response) { // todo }
});
}
(2) View层 :
【代码】
Map partMap = new HashMap<>();
if(mFilePath != null) { //有上传语音文件
audioFile=newFile(mFilePath);
RequestBody fileBody = RequestBody.create(MediaType.parse("audio/*"),audioFile);
partMap.put("voice\";filename=\""+audioFile.getName()+"",fileBody);
}
if( images != null && images.size() !=0 ) { //有上传图片
for(inti =0 ;i< images.size(); i++){
String path =images.get(i).path;
imagePaths.add(newFile(path));
} }
for(File file :imagePaths){
RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"),file);
partMap.put("pic\";filename=\""+ file.getName()+"",fileBody);
}
partMap.put("accessToken",RequestBody.create(null,"tTrLgDBA2lxdsHJOAZu9Sw=="));
partMap.put("memberId",RequestBody.create(null,"3"));
partMap.put("hotReply.problemId",RequestBody.create(null,mProblemId+""));
partMap.put("hotReply.content",RequestBody.create(null,mAnswerText));
partMap.put("hotReply.voiceDuration",RequestBody.create(null,mRecordTime+""));
mPresenter.upload(partMap) ; // 提交专家回答信息
【法2】
/**
* 上传多张图片及参数
*
* @param reqUrl URL地址
* @param params 参数
* @param pic_key 上传图片的关键字
* @param files 图片路径
*/
public ObservablesendMultipart(final String reqUrl, final Map params,
final String pic_key, final List files) {
return Observable.create(new Observable.OnSubscribe() {
@Override
public void call(final Subscriber subscriber) {
MultipartBody.Builder multipartBodyBuilder =new MultipartBody.Builder();
multipartBodyBuilder.setType(MultipartBody.FORM);
//遍历map中所有参数到builder
if (params !=null) {
for (String key :params.keySet()) {
multipartBodyBuilder.addFormDataPart(key, params.get(key));
}
}
//遍历paths中所有图片绝对路径到builder,并约定key如“upload”作为后台接受多张图片的key
if (files !=null) {
for (File file :files) {
multipartBodyBuilder.addFormDataPart(pic_key, file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
}
}
/*if(audioFile != null){
multipartBodyBuilder.addFormDataPart(pic_key, audioFile.getName(),RequestBody.create(MEDIA_TYPE_AUDIO,audioFile));
}*/
//构建请求体
RequestBody requestBody = multipartBodyBuilder.build();
Request.Builder RequestBuilder =new Request.Builder();
RequestBuilder.url(reqUrl);// 添加URL地址
RequestBuilder.post(requestBody);
Request request = RequestBuilder.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
subscriber.onError(e);
subscriber.onCompleted();
call.cancel();
}
@Override
public void onResponse(okhttp3.Call call, okhttp3.Response response)throws IOException {
String str = response.body().string();
subscriber.onNext(str);
subscriber.onCompleted();
call.cancel();
}
});
}
});
}