Okhttp3 in Android realizes uploading multiple pictures and transferring parameters at the same time
- 2021-07-22 08:24:48
- OfStack
Before uploading pictures, they were directly converted into io and transmitted to the server, instead of using the frame to transmit pictures.
Recently, I have done a project and plan to upload pictures in a different way.
With the development of Android, Okhttp is becoming more and more important, so this time I chose Okhttp to upload pictures.
Okhttp has been updated to Okhttp 3, and there are some differences in usage compared with before. I found a lot of information on the Internet,
And with java background colleagues repeatedly debugging, finally successfully upload a number of pictures, and at the same time pass some key-value pairs of parameters.
Here's my encapsulation of the process:
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
/**
* Upload multiple pictures and parameters
* @param reqUrl URL Address
* @param params Parameter
* @param pic_key Keywords for uploading pictures
* @param paths Picture path
*/
public Observable<String> sendMultipart(String reqUrl,Map<String, String> params,String pic_key, List<File> files){
return Observable.create(new Observable.OnSubscribe<String>(){
@Override
public void call(Subscriber<? super String> subscriber) {
MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
multipartBodyBuilder.setType(MultipartBody.FORM);
// Traversal map All parameters in the builder
if (params != null){
for (String key : params.keySet()) {
multipartBodyBuilder.addFormDataPart(key, params.get(key));
}
}
// Traversal paths The absolute path of all pictures in the builder , and agreed that key Such as " upload "Accepting multiple pictures in the background key
if (files != null){
for (File file : files) {
multipartBodyBuilder.addFormDataPart(pic_key, file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
}
}
// Build the request body
RequestBody requestBody = multipartBodyBuilder.build();
Request.Builder RequestBuilder = new Request.Builder();
RequestBuilder.url(reqUrl);// Add URL Address
RequestBuilder.post(requestBody);
Request request = RequestBuilder.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
subscriber.onError(e);
subscriber.onCompleted();
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
subscriber.onNext(str);
subscriber.onCompleted();
call.cancel();
}
});
}
});
}
Call in UI interface:
OkHttp3Utils.getInstance().sendMultipart(Constants.URL.URL_ADD_NOTICE, mMap, "appendix", mImageList)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable throwable) {
LogUtil.i(TAG, "throwable:" + throwable.toString());
}
@Override
public void onNext(String s) {
LogUtil.i(TAG, "s:" + s);
}
});
During debugging, multipartBodyBuilder. addFormDataPart (pic_key, file. getName (), RequestBody. create (MEDIA_TYPE_PNG, file); It is written as multipartBodyBuilder.addFormDataPart (pic_key, null, RequestBody.create (MEDIA_TYPE_PNG, file); As a result, the background can't get pictures in the conventional way (although the picture data can be seen when debugging breakpoints), which needs attention.