Android uses okhttp to upload image instance code

brief introduction

Uploading pictures is a common function of an app. It can be uploaded to alicloud through OOS or directly to the server background. OOS provides the corresponding SDK, which is ignored here. Next, upload pictures through okhttp

code

Directly upload the code uploadfilehelper.kt

Finally, the corresponding method can be called synchronously or asynchronously according to whether to take parameters

summary

First, it depends on whether to upload with parameters. If you do not upload with parameters, you can directly create a requestbody; If uploading with parameters, create multipartbody. Builder (), and then add all parameters addformdatapart. The addformdatapart method has a requestbody parameter, which is created by whether to monitor the progress. If the progress is required, rewrite the writeto() method of requestbody. If the progress is not monitored, directly create requestbody, and finally get requestbody by builder. Build()

A request object can be configured through the requestbody obtained in the previous step and the server path of the uploaded image.

Configure the request object in okhttpclient through. Newcall (request) to get the call object

Finally, call calls synchronous. Execute () or asynchronous. Enqueue (callback) to process the returned data in the callback.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>