通过改装发送字符串和图像 multipart/form-data
Sending string and images with retrofit multipart/form-data
我试图通过改造发送字符串和图像。
虽然我可以使用 x-www-form-urlencoded 和 hashmap 获得通过响应,但我需要将其与图像一起发送。所以我使用表单数据,但我无法获得具有相同名称和值的相同响应,在邮递员上对其进行了测试,它与我的 x-www-form 一样通过了。
邮递员来了
Postman request that got pass response
没有经过的方法
带表单数据
@Multipart
@POST("report")
fun push(
@HeaderMap headers: Map<String, String>,
@Part("store") string: RequestBody
): Call<ReportingResponse>
RequestBody.create(MediaType.parse("multipart/form-data"), "testing") //#1 fail
RequestBody.create(MediaType.parse("text/plain"), "testing") //#2 fail
我都试过了,都没有得到和邮递员一样的回复,看起来是这样的Retrofit request interceptor on Android Studio
通过 with x-www-form
的方法
@FormUrlEncoded
@POST("report")
fun push(
@HeaderMap headers: Map<String, String>,
@FieldMap form: MutableMap<String, Any>
): Call<ReportingResponse>
我该怎么办?
第 1 步:创建用于调用改造的接口方法 api
@POST(Const.Task_Ans_FILE_NAME)
Call<TaskInfoBean> verifyTaskAns(@Body RequestBody file);
第 2 步:使用以下代码将多部分图像数据与正文中的其他字段一起发送。
RetroFitService retroFitService = RetrofitClient.getAppData();
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (answer_type.equals("1")) {
builder.addFormDataPart(Const.ANSWER, answer);
} else {
try {
builder.addFormDataPart(Const.ANSWER, Const.SelectedFileName, RequestBody.create(MultipartBody.FORM, Const.BOS.toByteArray()));
}catch (Exception e){
Log.e(TAG, "doInBackground: "+e.getMessage() );
}
}
builder.addFormDataPart(Const.LOGIN_ID, login_id)
.addFormDataPart(Const.USER_ID, user_id)
.addFormDataPart(Const.PLAY_ID, play_id)
.addFormDataPart(Const.TASK_ID, task_id)
.addFormDataPart(Const.SCENARIO, scenario)
.addFormDataPart(Const.ANSWER_TYPE,answer_type)
.addFormDataPart(Const.SKIP, skip);
final RequestBody requestBody = builder.build();
Call<TaskInfoBean> call = retroFitService.verifyTaskAns(requestBody);
call.enqueue(new Callback<TaskInfoBean>() {
@Override
public void onResponse(Call<TaskInfoBean> call, Response<TaskInfoBean> response) {
if(response.code()==200) {
TaskInfoBean taskInfoBean = response.body();
listener.OnVerifyTaskAns(taskInfoBean);
}else{
Log.e(TAG, "onResponse: "+response.toString() );
}
}
@Override
public void onFailure(Call<TaskInfoBean> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.toString());
}
});
return null;
}
第 3 步:在您的 activity/fragment.
中调用此方法
我试图通过改造发送字符串和图像。
虽然我可以使用 x-www-form-urlencoded 和 hashmap 获得通过响应,但我需要将其与图像一起发送。所以我使用表单数据,但我无法获得具有相同名称和值的相同响应,在邮递员上对其进行了测试,它与我的 x-www-form 一样通过了。
邮递员来了 Postman request that got pass response
没有经过的方法 带表单数据
@Multipart
@POST("report")
fun push(
@HeaderMap headers: Map<String, String>,
@Part("store") string: RequestBody
): Call<ReportingResponse>
RequestBody.create(MediaType.parse("multipart/form-data"), "testing") //#1 fail
RequestBody.create(MediaType.parse("text/plain"), "testing") //#2 fail
我都试过了,都没有得到和邮递员一样的回复,看起来是这样的Retrofit request interceptor on Android Studio
通过 with x-www-form
的方法@FormUrlEncoded
@POST("report")
fun push(
@HeaderMap headers: Map<String, String>,
@FieldMap form: MutableMap<String, Any>
): Call<ReportingResponse>
我该怎么办?
第 1 步:创建用于调用改造的接口方法 api
@POST(Const.Task_Ans_FILE_NAME)
Call<TaskInfoBean> verifyTaskAns(@Body RequestBody file);
第 2 步:使用以下代码将多部分图像数据与正文中的其他字段一起发送。
RetroFitService retroFitService = RetrofitClient.getAppData();
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (answer_type.equals("1")) {
builder.addFormDataPart(Const.ANSWER, answer);
} else {
try {
builder.addFormDataPart(Const.ANSWER, Const.SelectedFileName, RequestBody.create(MultipartBody.FORM, Const.BOS.toByteArray()));
}catch (Exception e){
Log.e(TAG, "doInBackground: "+e.getMessage() );
}
}
builder.addFormDataPart(Const.LOGIN_ID, login_id)
.addFormDataPart(Const.USER_ID, user_id)
.addFormDataPart(Const.PLAY_ID, play_id)
.addFormDataPart(Const.TASK_ID, task_id)
.addFormDataPart(Const.SCENARIO, scenario)
.addFormDataPart(Const.ANSWER_TYPE,answer_type)
.addFormDataPart(Const.SKIP, skip);
final RequestBody requestBody = builder.build();
Call<TaskInfoBean> call = retroFitService.verifyTaskAns(requestBody);
call.enqueue(new Callback<TaskInfoBean>() {
@Override
public void onResponse(Call<TaskInfoBean> call, Response<TaskInfoBean> response) {
if(response.code()==200) {
TaskInfoBean taskInfoBean = response.body();
listener.OnVerifyTaskAns(taskInfoBean);
}else{
Log.e(TAG, "onResponse: "+response.toString() );
}
}
@Override
public void onFailure(Call<TaskInfoBean> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.toString());
}
});
return null;
}
第 3 步:在您的 activity/fragment.
中调用此方法