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

时间:2018-05-17 20:17:46

标签: php android retrofit

如何通过改造上传音频文件?

我带了一些我写的类和代码,但我没有回复这些代码

接口:

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

班级:

 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();
                        }
                    });

enter image description here

1 个答案:

答案 0 :(得分:0)

所有文件,无论是音频、视频、图像...在改造方面基本相同。它们只是文件。接口像任何其他文件一样用 @MultiPart.Part 注释。 查看 this 链接了解更多详情。