2017-01-28 1 views
0

Ich versuche, eine Videofile von meiner App hochladen.Nachrüstung2 Datei hochladen

Hier ist, was ich bisher habe:

public class Download extends Application { 

public interface upload { 
    @Multipart 
    @POST("new") 
    Call<Response> send(@Part("myFile") RequestBody file); 
} 

public void uploadFile(File xfile) { 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://192.168.0.3") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RequestBody file = RequestBody.create(MediaType.parse("video/*"), xfile); 
    upload xUpload = retrofit.create(upload.class); 
    Call<Response> call = xUpload.send(file); 

    try { 
     Response result = call.execute().body(); 


    } 
    catch (IOException e) 
    { 
     Log.d("TEST3", " didn't work "); 
    } 

} 




} 

ich folgende Fehlermeldung retrofit2.Response‘erhalten ist keine gültige Antwort Körperbau. Meintest du ResponseBody? für die Methode upload.send alle Ideen

Ich habe auf der Webseite von Retrofit2 nachgelesen und versuchte das Hauptbeispiel, das sie für das Hochladen einer Datei haben, aber es hat aus zwei Gründen nicht funktioniert. 1. Ich konnte nicht richtig finden ServiceGenerator 2. Meine Datei wurde in der Galerie gefunden und ich strömte den Inhalt in eine temporäre Datei, die ich hochladen soll, kann ich nicht direkt von seiner URI zugreifen ... oder Kann ich mit Retrofit2?

+1

https: // futur estud.io/tutorials/retrofit-2-how-to-upload-files-to-server – AnixPasBesoin

+0

Ich habe kürzlich das Hochladen von Dateien durchgeführt, indem ich diesem Tutorial https://futurestud.io/tutorials/retrofit-2-passing-multiple folgte -parts-zusammen mit einer Datei-mit-partmap – rajesh

Antwort

0

i verwenden Bild hochladen von Retrofit-2 wie folgt, es funktionierte richtig

File file = new File(image.getPath()); 
      RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file); 
      MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("gallery", file.getName(), mFile); 
      RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), id); 
      final NetworkCall networkCall=new NetworkCall(this); 
      Call<ResponseBody> call = networkCall.getRetrofit(false).uploadImage(filename, fileToUpload); 
      call.clone().enqueue(new Callback<ResponseBody>() { 
       @Override 
       public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 

       } 

       @Override 
       public void onFailure(Call<ResponseBody> call, Throwable t) { 

       } 
      }); 

hier ist Klasse mein Netzwerk Aufruf:

public class NetworkCall { 
    Context context; 
    ProgressDialog progressDialog; 
    public NetworkCall(Context context){ 
     this.context = context; 
    } 

    public IApi getRetrofit(boolean isShowLoading){ 

     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     httpClient.connectTimeout(0, TimeUnit.SECONDS).readTimeout(0,TimeUnit.SECONDS); 

     httpClient.addInterceptor(new Interceptor() { 
             @Override 
             public Response intercept(Chain chain) throws IOException { 
              Request original = chain.request(); 

              Request request = original.newBuilder() 
                .header("Content_type","application/json") 
                .header("Accept", "application/json") 
                .method(original.method(), original.body()) 
                .build(); 

              return chain.proceed(request); 
             } 
            }); 
     Gson gson = new GsonBuilder() 
       .setLenient() 
       .create(); 

       OkHttpClient client = httpClient.build(); 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(Constants.BASE_URL) 
       .client(client) 
       .addConverterFactory(GsonConverterFactory.create(gson)) 
       .build(); 
     if (isShowLoading&&context instanceof BaseActivity) 
      showLoading(); 
     // prepare call in Retrofit 2.0 

     IApi api = retrofit.create(IApi.class); 
//  Call<BaseResponce> call = api.callService(json); 
     //asynchronous call 
//  call.enqueue(this); 
     return api; 
    } 
    private void showLoading(){ 
     try { 
      ((BaseActivity)context).runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        progressDialog = new ProgressDialog(context); 
        progressDialog.setMessage("Please wait..."); 
        progressDialog.setCancelable(false); 
        progressDialog.show(); 
       } 
      }); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public void dismissLoading(){ 
     try { 
      ((BaseActivity)context).runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        progressDialog.cancel(); 
        progressDialog.dismiss(); 
       } 
      }); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

Ich benutze diese in IAPI Klasse

@Multipart 
    @POST("events/file_upload.json") 
    Call <ResponseBody> uploadImage(@Part("event_id") RequestBody id,@Part MultipartBody.Part part); 

hoffe es hilft