这里我需要通过授权承载来获取服务器的响应,以便将文件上传到服务器我正在使用改造。
我试过两种方式
1)
这是我在改造界面类
中初始化的方式@POST("document/kycDocument/user/3")
Call<UploadKycpojo> uploadkycdoc(@Header("Authorization")String token, @Body UploadKycRequest uploadKycRequest);
这是我从界面类
中调用它的方式Call<UploadKycpojo> request = RestClient.getInstance(UploadIdActivtiy.this).get().uploadkycdoc("Bearer "+auth,uploadKycRequest);
2)
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token)
.build();
return chain.proceed(newRequest);
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(/** your url **/)
.addConverterFactory(GsonConverterFactory.create())
.build();
任何帮助将不胜感激。 在此先感谢!
答案 0 :(得分:2)
我确实试过,它为我工作请参考以下代码:
@Multipart
@POST("document/kycDocument/user/3")
Call<UploadKycpojo> uploadkycdoc(@Header("Authorization")String token, @Part
MultipartBody.Part file, @PartMap() Map<String,
RequestBody> partMap);
对于API调用,请使用以下方法:
private void uploadkycdoc() {
MultipartBody.Part filePart;
HashMap<String, RequestBody> requestBodyMap = new HashMap<>();
requestBodyMap.put("imageSlide", RequestBody.create(MediaType.parse("multipart/form-data"), "front"));
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<UploadKycpojo> uploadkycdocCall = null;
File file = new File(getRealPathFromURI(fileUri, context));
RequestBody requestFile = RequestBody.create(MediaType.parse("*/*"), file);
filePart= MultipartBody.Part.createFormData("file", file.getName(),
requestFile);
uploadkycdocCall = apiInterface.uploadkycdoc("Bearer " + token, filePart, requestBodyMap);
uploadkycdocCall.enqueue(new Callback<UploadKycpojo>() {
@Override
public void onResponse(Call<UploadKycpojo> call, Response<UploadKycpojo> response) {
cancelProgressDialog();
try {
if (response.isSuccessful()) {
} else {
}
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<UploadKycpojo> call, Throwable t) {
}
});
}
答案 1 :(得分:1)
您的改造界面方法应如下所示: -
@Multipart
@POST("document/kycDocument/user/3")
Call<UploadKycpojo> uploadkycdoc(@Header("Authorization")String token, @Part
MultipartBody.Part file);
你的通话声明如下: -
File file = new File(yourStringPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), getRealPathFromURI(data.getData()));
MultipartBody.Part multipartBody =MultipartBody.Part.createFormData("file",file.getName(),requestFile);
Call<UploadKycpojo> request = RestClient.getInstance(UploadIdActivtiy.this).get()
.uploadkycdoc("Bearer "+auth,multipartBody );
答案 2 :(得分:0)
您只需要在Bearer
之前添加空间,对我来说就可以尝试一下:
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", " Bearer " + token)
.build();
return chain.proceed(newRequest);
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(/** your url **/)
.addConverterFactory(GsonConverterFactory.create())
.build();