【问题标题】:retrofit2 upload file改造2上传文件
【发布时间】:2017-01-28 00:48:56
【问题描述】:

我正在尝试从我的应用上传视频文件。

这是我目前所得到的:

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 吗?对于方法上传。发送任何想法

我已经阅读了 retrofit2 网页,并尝试了他们上传文件的主要示例,但由于两个原因它没有工作。 1. 找不到合适的ServiceGenerator 2. 我的文件在图库中找到,我将其内容流式传输到我要上传的临时文件,我无法直接从其 URI 访问它...或者我可以使用 retrofit2 吗?

【问题讨论】:

标签: android retrofit2


【解决方案1】:

我曾经像这样从改造 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);

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2015-12-27
    • 2017-01-31
    • 2018-12-21
    • 2016-10-11
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多