Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Retrofit upload function for Android

2025-04-02 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report