简体   繁体   English

如何使用 Retrofit2 Android 将音频 (Mp3) 上传到服务器

[英]How to upload Audio (Mp3) to Server Using Retrofit2 Android

How to upload an audio file by retrofit?如何通过改造上传音频文件?

I brought some of the classes and code I wrote, but I did not respond to these codes我带来了一些我写的类和代码,但是我没有回应这些代码

Interface:界面:

 @Multipart
    @POST("comments")
    Call<Wallet> setComent(@Part("voice") RequestBody song, @Part("api_token") String api_token, @Part("product") String product, @Part("body") String body);

Class :班级 :

 AudioSavePathInDevice =
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                        "NAME" + "AudioRecording.mp3";


    File file = new File(AudioSavePathInDevice );

                    //creating request body for file
                    RequestBody requestFile = RequestBody.create(MediaType.parse("audio/*"), file);

                    MyInterFace myInterFace = MyServices.createService(MyInterFace.class);
                    Call<Wallet> calling = myInterFace.setComent
                            (requestFile,new AppStore(getContext()).LoadMyShereKES(AppStore.Keys_TOKEN),AppStore.ID_BACHGROUD,Ediet.getText().toString());

                    calling.enqueue(new Callback<Wallet>()
                    {
                        @Override
                        public void onResponse( Call<Wallet> call,  Response<Wallet> response)
                        {
                            appStore.CloseDilag();
                        }

                        @Override
                        public void onFailure(Call<Wallet> call, Throwable t)
                        {

                            appStore.CloseDilag();
                        }
                    });

在此处输入图片说明

All files, whether audio, video, images... are basically the same for retrofit.所有文件,无论是音频、视频、图像……在改造方面基本相同。 They are just files.它们只是文件。 The interface is annotated with @MultiPart.Part just like any other file.接口像任何其他文件一样用@MultiPart.Part注释。 check this link for more details.查看链接了解更多详情。

How to upload an audio file by retrofit?如何通过改造上传音频文件?

I brought some of the classes and code I wrote, but I did not respond to these codes我带来了一些我编写的类和代码,但是我没有对这些代码做出回应

Interface:界面:

 @Multipart
    @POST("comments")
    Call<Wallet> setComent(@Part("voice") RequestBody song, @Part("api_token") String api_token, @Part("product") String product, @Part("body") String body);

Class :班级 :

 AudioSavePathInDevice =
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                        "NAME" + "AudioRecording.mp3";


    File file = new File(AudioSavePathInDevice );

                    //creating request body for file
                    RequestBody requestFile = RequestBody.create(MediaType.parse("audio/*"), file);

                    MyInterFace myInterFace = MyServices.createService(MyInterFace.class);
                    Call<Wallet> calling = myInterFace.setComent
                            (requestFile,new AppStore(getContext()).LoadMyShereKES(AppStore.Keys_TOKEN),AppStore.ID_BACHGROUD,Ediet.getText().toString());

                    calling.enqueue(new Callback<Wallet>()
                    {
                        @Override
                        public void onResponse( Call<Wallet> call,  Response<Wallet> response)
                        {
                            appStore.CloseDilag();
                        }

                        @Override
                        public void onFailure(Call<Wallet> call, Throwable t)
                        {

                            appStore.CloseDilag();
                        }
                    });

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM