2017-01-28 69 views
0

我想從我的應用程序上傳一個videofile。retrofit2上傳文件

這裏是我到目前爲止有:

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

} 




} 

我收到以下錯誤retrofit2.Response」不是一個有效的響應身型。你是說ResponseBody?方法upload.send任何想法

我已經閱讀了retrofit2網頁,並嘗試了他們有上傳文件的主要例子,但它沒有工作的原因有兩個。 1.我找不到合適的ServiceGenerator 2.我的文件在Gallery中找到,我將其內容傳輸到一個臨時文件,我要上傳,我無法直接從它的URI訪問它...或者我可以用retrofit2嗎?

+1

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

+0

我已經完成了文件上傳最近按照本教程 https://futurestud.io/tutorials/retrofit-2-passing-multiple - 零件 - 沿一個文件與 - partmap – rajesh

回答

0

我用它來從改造2這樣的上傳圖片,它的工作正確

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

       } 
      }); 

這裏是我的網絡電話類:

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

我用這個在IAPI類

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

希望它有幫助