Reputation: 492
I'm trying to upload my .mp3 file to soundcloud
using this retrofit2
interface :
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);
}
and realization with interceptor to include token:
@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();
}
Getting response:
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());
}
});
After request i'm receiving error 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
Also tried passing file
and MultipartBody.Part
. In case of multipart i`m receiving error 422. So what is the right way to upload audio to soundcloud? I guess that i'm passing audio in wrong format and server doesn't recognise it.
Upvotes: 1
Views: 1530
Reputation: 584
Here example for upload image with retrofit 2. First, when create retrofit for multipart request, doesn't add converter factory.
public static APIMultipartService getMultipartService() {
if (multipartService == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(DOMAIN)
.build();
multipartService = retrofit.create(APIMultipartService.class);
}
return multipartService;
}
Second, use RequestBody in interface.
public interface APIMultipartService {
@POST("/api/v1/job/photo/add")
Call<ResponseBody> uploadJobPhoto(@Body RequestBody body);}
And here example for create request body with file.
RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
builder.addFormDataPart("pic", "photo.png", body);
builder.addFormDataPart("jobId", id);
builder.addFormDataPart("privateKey", privateKey);
Call<ResponseBody> request = ApiService.getMultipartService().uploadJobPhoto(builder.build());
request.enqueue(callbackPhotoRequest);
Try like that, maybe help you.
Upvotes: 2