我使用改装后的文字数据,单一的图像和多个图像在单一的职位要求。我试过一些方法,但对我不起作用。我已经附上 Postman 截图和以前的代码,我做了下面。
Postman 截图
我尝试过的示例代码:
API接口类:
public interface PostSurveyFormApiInterface {
@Multipart
@POST("Shared/InsertDirectSurveyAsync")
Call<ResponseBody> postDirectSurveyForm(@Header("Authorization") String auth,
@Header("Content-Type") String contentType,
@Part("CompanyName") RequestBody companyName,
@Part("Address") RequestBody address,
@Part MultipartBody.Part digitalStamp,
@Part MultipartBody.Part digitalSignature,
@Part MultipartBody.Part[] surveyImage);
}
发布数据的方法:
private void postDataToServer(List<Uri> paths){
RequestBody companyName = RequestBody.create(MediaType.parse("text/plain"), edtCompanyName.getText().toString().trim());
RequestBody address = RequestBody.create(MediaType.parse("text/plain"), edtCompanyAddress.getText().toString());
//for single stamp image
File fileStamp = new File(getRealPathFromURI(stampUri));
RequestBody requestBodyStamp = RequestBody.create(MediaType.parse("image/*"),fileStamp);
MultipartBody.Part stampImagePart = MultipartBody.Part.createFormData("DigitalStamp",
fileStamp.getName(),
requestBodyStamp);
//for single signature image
File fileSignature = new File(getRealPathFromURI(signatureUri));
RequestBody requestBodySignature = RequestBody.create(MediaType.parse("image/*"),fileSignature);
MultipartBody.Part signatureImagePart = MultipartBody.Part.createFormData("DigitalSignature",
fileSignature.getName(),
requestBodySignature);
//for multiple survey(files) images
MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[paths.size()];
for (int index = 0; index < paths.size(); index++) {
Log.v(TAG,"survey image path \n"+getRealPathFromURI(paths.get(index)));
File file = new File(getRealPathFromURI(paths.get(index)));
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"), file);
surveyImagesParts[index] = MultipartBody.Part.createFormData("Files", file.getName(), surveyBody);
}
PostSurveyFormApiInterface apiInterface = ApiClient.getApiClient().create(PostSurveyFormApiInterface.class);
apiInterface.postDirectSurveyForm(
getToken(),
"application/json",
companyName,
address,
stampImagePart,
signatureImagePart,
surveyImagesParts
).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
progressDialog.dismiss();
if (response.isSuccessful()){
Log.v(TAG,"response successful");
}else{
Log.v(TAG,"failed to post data");
Log.v(TAG,"error : "+response.toString());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
progressDialog.dismiss();
Log.e(TAG,"error : "+t.getMessage());
}
});
}
请帮助我的家伙,我正在尝试这从过去3天,但无法修复。提前谢谢。
1条答案
按热度按时间sbtkgmzw1#
经过长时间的研究,以下代码适用于我。。。
PostSurveyFormapi接口
过帐数据的方法
preparefilepart方法:
getrealpathfromuri方法: