2017-01-28 11 views
0

私のアプリからビデオファイルをアップロードしようとしています。ここで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のWebページを読んで、ファイルをアップロードするための主な例を試しましたが、2つの理由で動作しませんでした。 1.適切なServiceGeneratorが見つかりません 2.私のファイルがギャラリーに見つかりました。アップロードする一時ファイルにそのコンテンツをストリーミングしました。その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 -parts-along-a-file-with-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(); 
     } 
    } 
} 

iがIAPIクラスでこれを使用

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

願っています