如何使用改造 2 通过我的 android 应用程序将音频上传到 soundcloud
How to upload audio to soundcloud via my android app using retrofit 2
我正在尝试使用此 retrofit2
界面将我的 .mp3 文件上传到 soundcloud
:
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
还尝试通过 file
和 MultipartBody.Part
。如果是多部分,我会收到错误 422。那么将音频上传到 soundcloud 的正确方法是什么?我想我传递的音频格式错误,服务器无法识别它。
这里是使用改造 2 上传图片的例子。
首先,为多部分请求创建改造时,不添加转换器工厂。
public static APIMultipartService getMultipartService() {
if (multipartService == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(DOMAIN)
.build();
multipartService = retrofit.create(APIMultipartService.class);
}
return multipartService;
}
其次,在接口中使用RequestBody。
public interface APIMultipartService {
@POST("/api/v1/job/photo/add")
Call<ResponseBody> uploadJobPhoto(@Body RequestBody body);}
这里是使用文件创建请求正文的示例。
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);
试试看,也许能帮到你。
我正在尝试使用此 retrofit2
界面将我的 .mp3 文件上传到 soundcloud
:
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
还尝试通过 file
和 MultipartBody.Part
。如果是多部分,我会收到错误 422。那么将音频上传到 soundcloud 的正确方法是什么?我想我传递的音频格式错误,服务器无法识别它。
这里是使用改造 2 上传图片的例子。 首先,为多部分请求创建改造时,不添加转换器工厂。
public static APIMultipartService getMultipartService() {
if (multipartService == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(DOMAIN)
.build();
multipartService = retrofit.create(APIMultipartService.class);
}
return multipartService;
}
其次,在接口中使用RequestBody。
public interface APIMultipartService {
@POST("/api/v1/job/photo/add")
Call<ResponseBody> uploadJobPhoto(@Body RequestBody body);}
这里是使用文件创建请求正文的示例。
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);
试试看,也许能帮到你。