2016-05-13 6 views
0

Ich versuche zu Soundcloud zu meiner MP3-Datei zu soundcloud mit dieser retrofit2 Schnittstelle laden:Wie Audio-Upload über meinen Android-App mit Nachrüst-2

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

und Realisierung mit Interceptor Token enthalten:

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

Erste Antwort:

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

Nach Anfrage ich bin receivin g Fehler 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 

versuchte auch file und MultipartBody.Part vorbei. Im Falle von Multipart erhalte ich den Fehler 422. Was ist der richtige Weg um Audio zu Soundcloud hochzuladen? Ich vermute, dass ich Audio im falschen Format übergebe und der Server erkennt es nicht.

Antwort

0

Hier Beispiel für Upload-Bild mit Nachrüstung 2. Erstens, wenn Nachrüstung für mehrteilige Anfrage erstellen, fügt Konverter Factory nicht hinzu.

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

Zweitens, verwenden Sie RequestBody in der Schnittstelle.

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

Und hier Beispiel zum Erstellen von Anfrage Körper mit Datei.

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

Versuchen Sie so, vielleicht Ihnen helfen.

+0

Funktioniert nicht für mich. Server lehnt es trotzdem ab –

+0

Können Sie Server Seite debuggen? Vielleicht gibt es ein Problem. Ursache 500 Fehler ist kein clientseitiges Problem. –

Verwandte Themen