In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will introduce in detail for you "Android how to upload files using Retrofit", the content steps are clear and detailed, and the details are handled properly, and the editor updates different knowledge points every day. I hope that this article "Android how to use Retrofit to upload files" can give you unexpected gains, please follow the editor's ideas slowly in depth, the specific content is as follows, to harvest new knowledge.
The details are as follows
Package RetrofitManagerpublic class RetrofitManager {private static RetrofitManager retrofitManager; private Retrofit retrofit; private RetrofitManager () {} public static RetrofitManager getInstance () {if (retrofitManager = = null) {synchronized (RetrofitManager.class) {if (retrofitManager = = null) {retrofitManager = new RetrofitManager ();} return retrofitManager } public Retrofit getRetrofit () {if (retrofit = = null) {/ / add log interceptor HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor (); / / intercept level is body (can print out a complete network request) httpLoggingInterceptor.level (HttpLoggingInterceptor.Level.BODY) / / use OkHttpClient OkHttpClient okHttpClient = new OkHttpClient.Builder () .addInterceptor (httpLoggingInterceptor) .connectTimeout (1Magna TimeUnit.MINUTES) .readTimeout (1memeUnit.MINUTES) .build () / / create Retrofit retrofit = new Retrofit.Builder () / / use the Gson conversion factory .addConverterFactory (GsonConverterFactory.create ()) .addCallAdapterFactory (RxJava2CallAdapterFactory.create ()) / / basic Url .baseUrl ("http://* *. *: * / ") .client (okHttpClient) .build () } return retrofit;}} II. Upload a single file
1. Declare a method in the Api interface
@ Multipart@POST ("fileUpload") Observable upload (@ Part List parts)
two。 Instantiate the api interface
/ / instantiate api interface Api api = RetrofitManager.getInstance () .getRetrofit () .create (Api.class)
3. Construction parameters
File file = new File ("/ sdcard/DCIM/Camera/**.jpg"); RequestBody body = RequestBody.create (MediaType.parse ("multipart/form-data"), file); MultipartBody multipartBody = new MultipartBody.Builder () .addFormDataPart ("file", "fileName.jpg", body) .setType (MultipartBody.FORM) .build ()
4. Submit a request
Api.upload (parts) .subscrieOn (AndroidSchedulers.mainThread ()) .subscribeOn (Schedulers.io ()) .subscribe (new Observer () {@ Override public void onNext (String s) {Log.i ("- -", s) / / request result} @ Override public void onError (Throwable e) {} @ Override public void onComplete () {}}); 3. Upload multiple files
1. Declare a method in the Api interface
@ Multipart@POST ("fileUploadMore") Observable uploadMore (@ PartMap Map multiMap)
two。 Instantiate the api interface
/ / instantiate api interface Api api = RetrofitManager.getInstance () .getRetrofit () .create (Api.class)
3. Construction parameters
File file = new File ("/ sdcard/DCIM/Camera/**.jpg"); RequestBody body = RequestBody.create (MediaType.parse ("multipart/form-data"), file); MultipartBody multipartBody1 = new MultipartBody.Builder () .addFormDataPart ("file", "fileName1.jpg", body) .setType (MultipartBody.FORM) .build () MultipartBody multipartBody2 = new MultipartBody.Builder () .addFormDataPart ("file", "fileName2.jpg", body) .setType (MultipartBody.FORM) .build (); MultipartBody multipartBody3 = new MultipartBody.Builder () .addFormDataPart ("file", "fileName3.jpg", body) .setType (MultipartBody.FORM) .build () MultipartBody multipartBody4 = new MultipartBody.Builder () .addFormDataPart ("file", "fileName4.jpg", body) .setType (MultipartBody.FORM) .build (); / / put all files into the map collection Map parts = new HashMap (); parts.put ("F1", multipartBody1.parts ()); parts.put ("f2", multipartBody2.parts ()); parts.put ("f3", multipartBody3.parts ()) Parts.put ("f4", multipartBody4.parts ())
4. Submit a request
Api.uploadMore (parts) .subscrieOn (AndroidSchedulers.mainThread ()) .subscribeOn (Schedulers.io ()) .subscribe (new Observer () {@ Override public void onNext (String s) {Log.i ("- -", s) / / request result} @ Override public void onError (Throwable e) {} @ Override public void onComplete (); what is Android? Android is a free and open source operating system based on the Linux kernel, mainly used in mobile devices, such as smartphones and tablets, led and developed by Google and the Open Mobile Alliance.
If you can read this, the editor hopes that you will have the most profound understanding of the key issue of "how Android uses Retrofit to upload files" from the practical level, and you need to practice and use it yourself in order to understand it. If you want to read more related articles, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.