retrofit2用于多部分文件上传工作在一个类但不是多个类

时间:2018-03-12 15:02:08

标签: java android retrofit2 multipartform-data

我正在使用retrofit2进行多部分文件上传。但是当我以两种形式使用下面的代码时,我收到一个错误。我在登录后使用代码上传个人资料创建和更新。如果我只在更新配置文件或注册页面中使用它,它工作正常。

我的代码[Client + Interface + Main class]:

public class RetrofitClient {
private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

public interface FileUploadService {

@Multipart
@POST("ProfileFileUpload")
Call<ResponseBody> uploadFile(
        @Part("authToken") RequestBody authToken,
        @Part MultipartBody.Part docFile,
        @Part("FileName") RequestBody FileName,
        @Part("TypeName") RequestBody TypeName);
}

private void executeMultipartPost(String picturePath){
    try {

            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Retrofit retrofitProfile = new Retrofit.Builder()
                                    .baseUrl("url")
                                    .addConverterFactory(GsonConverterFactory.create())
                                    .client(httpClient)
                                    .build();

        mFileUploadService = retrofitProfile.create(FileUploadService.class);

        File file = new File(Environment.getExternalStorageDirectory(),picturePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("docFile", file.getName(), requestFile);
        RequestBody autoToken = RequestBody.create(MediaType.parse("text/plain"), "xxxxxxxxxx");
        RequestBody FileName = RequestBody.create(MediaType.parse("text/plain"), file.getName());
        RequestBody FileType = RequestBody.create(MediaType.parse("text/plain"),  "image");

        Call<ResponseBody> req = mFileUploadService.uploadFile(autoToken, body,FileName,FileType);
        req.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()) {
                        String Response="";
                        // dont work
                        String objRes = response.body().string();
                        JSONObject jsonObject = new JSONObject(objRes);
                        profileImage = jsonObject.optString("response").toString();
                        Response = jsonObject.optString("status").toString();
                        dialog.dialogBoxWithAlert(ActivityCreateAccount.this,"Alert", Response);

                    } else {
                        // works on failure
                        dialog.dialogBoxWithAlert(ActivityCreateAccount.this,"Error", "There is a error");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                progDailog.dismiss();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                progDailog.dismiss();
                t.printStackTrace();
            }
        });

    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
    }
}

Crashlog

  

03-13 22:32:33.447 23389-23389 / com.source.websys.rwm A / art:art / runtime / entrypoints / quick / quick_trampoline_entrypoints.cc:2077]检查失败:instr_code ==指令:: INVOKE_INTERFACE || instr_code ==指令:: INVOKE_INTERFACE_RANGE对接口trampoline的意外调用:invoke-virtual {v1,v127,v2646493288,v85,v48},thing @ 13744

0 个答案:

没有答案