compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
网上有好多例子,不多说了,主要是两个part参数,直接上代码:
1、接口的声明:
/**
* 单个文件上传
* @param url
* @param description
* @param file
* @return
*/
@Multipart
@POST()
Call uploadFile(@Url String url,
@Part("description") RequestBody description,
@Part MultipartBody.Part file);
接口的调用:
/**
* 文件上传
*
* @param filePath 上传文件的路径
*/
public void fileUpload(String filePath) {
if (!NetworkState.isNetworkAvailable(MyApplication.getMyApplication())) {
return;
}
//构建要上传的文件
File file = new File(filePath);
RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("aFile", file.getName(), requestFile);
String descriptionString = "This is a file description";
RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
SettingsInfo info = DataManager.getInstance().getSettingsInfo();
String url = "http://" + Const.SERVER_ADDR + "/n/as/grapher_upload_file";
Call call = ServiceGenerator.createService(FileUploadClient.class).uploadFile(url, description, body);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.d("lixm", "fileUpload() , httpResult = " + httpResult);
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d("lixm", "fileUpload(), onFailure, error = " + t.getMessage());
}
});
}
1、接口的声明
/**
* 使用@Multipart注解方法,并用@Part注解方法参数,类型是List
* @param url
* @return
*/
@Multipart
@POST()
Observable uploadFiles4MultiBodyPars(@Url String url,@Part() List parts);
2、调用方法
/**
* 多文件上传
*
* @param files 上传文件的集合
* @param fileUploadParam 上传的信息
*/
public void multiFileUpload(List files, FileUploadParam fileUploadParam, Observer fileUploadbserver) {
if (!NetworkState.isNetworkAvailable(MyApplication.getMyApplication())) {
return;
}
SettingsInfo info = DataManager.getInstance().getSettingsInfo();
String url = "http://" + info.libServerAddr + "/n/as/grapher_upload_file";
List.Part> parts = FileUploadUtils.filesToMultipartBodyParts(files,fileUploadParam);
FileUploadClient client = ServiceGenerator.createService(FileUploadClient.class);
Observable observable = client.uploadFiles4MultiBodyPars(url, parts);
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fileUploadbserver);
}
public static List.Part> filesToMultipartBodyParts(List files,FileUploadParam fileUploadParam) {
// 上传图片信息
List.Part> parts = new ArrayList<>(files.size());
for (int i = 0; i < files.size() ; i ++) {
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), files.get(i));
MultipartBody.Part part = MultipartBody.Part.createFormData("file"+ i, files.get(i).getName(), requestBody);
parts.add(part);
}
// 上传文字信息
if(fileUploadParam != null){
MultipartBody.Part equipmentId = MultipartBody.Part.createFormData("equipmentId", fileUploadParam.equipmentId);
parts.add(equipmentId);
MultipartBody.Part acceptanceId = MultipartBody.Part.createFormData("acceptanceId", fileUploadParam.acceptanceId);
parts.add(acceptanceId);
MultipartBody.Part fileName = MultipartBody.Part.createFormData("fileName", fileUploadParam.fileName);
parts.add(fileName);
MultipartBody.Part uploadPerson = MultipartBody.Part.createFormData("uploadPerson", fileUploadParam.uploadPerson);
parts.add(uploadPerson);
MultipartBody.Part timestamp = MultipartBody.Part.createFormData("timestamp", fileUploadParam.timestamp);
parts.add(timestamp);
String strMd5 = MD5Util.getMD5(fileUploadParam.equipmentId + fileUploadParam.timestamp + Const.KEY_MD5);
MultipartBody.Part secStr = MultipartBody.Part.createFormData("secStr", strMd5);
parts.add(secStr);
}
return parts;
}
public class ServiceGenerator {
private static final String BASE_URL = "https://api.github.com/";
private static Retrofit retrofit;
// 1 创建Retrofit.Builder
private static Retrofit.Builder builder
= new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
//.addConverterFactory(new FileRequestBodyConverterFactory()) // 支持文件上传时内容的转换
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()); // 支持RxJava;
// 2 创建OkHttpClient
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// 日志拦截器
private static HttpLoggingInterceptor logging = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
// test
private static LogInterceptor logInterceptor = new LogInterceptor();
// 3 创建service_client
public static S createService(Class serviceClass) {
if (!httpClient.interceptors().contains(logInterceptor)) {
httpClient.addInterceptor(logInterceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
return retrofit.create(serviceClass);
}
}
@FormUrlEncoded()
@POST()
Call uploadVideoInfoByAddBatch(@Url String url,
@FieldMap Map requestMap);
注意:@FormUrlEncoded()要和@FieldMap配合使用,否则的话会报错。