![](/img/trans.png)
[英]How to upload audio in soundcloud via my app and i want to send that id also to my server
[英]How to upload audio to soundcloud via my android app using retrofit 2
我想上傳我的.MP3文件soundcloud
使用此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
還嘗試傳遞file
和MultipartBody.Part
。 在 multipart 的情況下,我收到錯誤 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);
試試看,也許對你有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.