如何使用改造2通过我的Android应用程序将音频上传到soundcloud

时间:2016-05-13 09:44:41

标签: android upload retrofit soundcloud

我尝试使用此soundcloud界面将我的.mp3文件上传到retrofit2

public interface ApiInterface {
@Multipart
@POST("tracks?client_id=********&client_secret=********")
Call<ResponseBody> uploadTrack(@Part("track") RequestBody file, @Part("track[title]") String title, @Part("track[sharing]") String sharing);
}

并使用拦截器实现包含令牌:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initComponents();

    sharedPreferences = getSharedPreferences(getApplicationInfo().name, MODE_PRIVATE);
    token = sharedPreferences.getString(Config.ACCESS_TOKEN, "");

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Config.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(createOkHttpWithAuth())
            .build();
    apiInterface = retrofit.create(ApiInterface.class);
}
private OkHttpClient createOkHttpWithAuth() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    }
    return new OkHttpClient.Builder()
            .addInterceptor(new SoundcloudInterceptor(token))
            .addNetworkInterceptor(interceptor)
            .build();
}

获得回应:

            File file = new File(Environment.getExternalStorageDirectory() + "/Music/test.mp3");

            // create RequestBody instance from file
            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);

            // MultipartBody.Part is used to send also the actual file name
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("audio", file.getName(), requestFile);

             Call<ResponseBody> call =  apiInterface.uploadTrack(requestFile, "Test", "private");
             call.enqueue(new Callback<ResponseBody>() {

                 @Override
                 public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                     //Log.v("Upload", "success" + response.body().toString());
                 }

                 @Override
                 public void onFailure(Call<ResponseBody> call, Throwable t) {
                     Log.e("Upload error:", t.getMessage());
                 }
             });

请求后我收到错误500:

05-13 12:36:23.407 6540-7186/com.example.soundcloud_app_android D/OkHttp: <-- 500 Internal Server Error https://api.soundcloud.com/tracks?client_id=**********&client_secret=**********&oauth_token=********** (3455ms)
05-13 12:36:23.407 6540-7186/com.example.soundcloud_app_android D/OkHttp: Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Access-Control-Allow-Methods: GET, PUT, POST, DELETE
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Access-Control-Allow-Origin: *
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Access-Control-Expose-Headers: Date
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Cache-Control: no-cache
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Content-Type: application/json; charset=utf-8
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Date: Fri, 13 May 2016 09:34:26 GMT
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Server: am/2
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Status: 500 Internal Server Error
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: Content-Length: 60
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: OkHttp-Sent-Millis: 1463132179952
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: OkHttp-Received-Millis: 1463132183407
05-13 12:36:23.408 6540-7186/com.example.soundcloud_app_android D/OkHttp: <-- END HTTP

还尝试传递fileMultipartBody.Part。如果多部分我收到错误422.那么将音频上传到soundcloud的正确方法是什么?我想我以错误的格式传递音频,服务器无法识别它。

1 个答案:

答案 0 :(得分:1)

这里有关于改装2的上传图片的示例。 首先,在为多部分请求创建改造时,不添加转换器工厂。

::i

其次,在界面中使用RequestBody。

public static APIMultipartService getMultipartService() {
    if (multipartService == null) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(DOMAIN)
                .build();
        multipartService = retrofit.create(APIMultipartService.class);
    }
    return multipartService;
}

这里是带文件的创建请求主体的示例。

public interface APIMultipartService {
    @POST("/api/v1/job/photo/add")
    Call<ResponseBody> uploadJobPhoto(@Body RequestBody body);}

尝试这样,也许可以帮到你。