将 android(retrofit) 中的图像和参数上传到 django 得到错误的请求
Upload Image and parameters from android(retrofit) to django get bad request
我这样用API;
@POST("api/info/save")
Call<ResponseBody> uploadMultiFile(@Body RequestBody file);
此函数从数组列表中获取图像并添加参数以发送服务器;
private void uploadMultiFile(final String event_date, final int event_type, final int coordinate_x, final int coordinate_y) {
String url = Constant.baseUrl;
Coordinate coordinate = new Coordinate();
coordinate.setX(coordinate_x);
coordinate.setY(coordinate_y);
TokenInterceptor interceptor=new TokenInterceptor();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(url)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Api service = retrofit.create(Api .class);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("event_date", event_date);
builder.addFormDataPart("event_type", String.valueOf(event_type));
builder.addFormDataPart("coordinate", String.valueOf(coordinate));
// Map is used to multipart the file using okhttp3.RequestBody
// Multiple Images
for (int i = 0; i < imagePathList.size(); i++) {
File file = new File(imagePathList.get(i));
builder.addFormDataPart("image[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
MultipartBody requestBody = builder.build();
Call<ResponseBody> call = service.uploadMultiFile(requestBody);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(MMEActivity.this, "Success " + response.message(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("d", "Error " + t.getMessage());
}
});
}
我遇到错误请求错误。我怎么解决这个问题?我在后端使用 Django。这是我从请求中获取图像和参数的函数。
@api_view(['POST'])
def eventInfoSave(request):
event_date = request.data['event_date']
print(request.data)
.
.
if bool(request.FILES.getlist('image', False)) == True:
uploaded_file = request.FILES.getlist('image')
当我使用邮递员时没有任何问题。我可以从一个键获取所有图像。
我使用解决了我的问题;
@Multipart
@POST("/save")
Call<Response> uploadImage(@Part MultipartBody.Part[] image, @Part("event_date") RequestBody event_date,
@Part("event_type") RequestBody event_type,
.
.
.);
我将我的功能更改为;
private void uploadMultiFile(final String event_date, final int event_type, final int coordinate_x, final int coordinate_y) {
String url = Constant.baseUrl;
Coordinate coordinate_ = new Coordinate();
coordinate_.setX(coordinate_x);
coordinate_.setY(coordinate_y);
String coordinate__ = new Gson().toJson(coordinate_);
TokenInterceptor interceptor=new TokenInterceptor();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(url)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Api service = retrofit.create(Api .class);
MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[imagePathList.size()];
for (int index = 0; index <
imagePathList.size(); index++) {
Log.d("ss",
"requestUploadSurvey: survey image " +
index +
" " +imagePathList.get(index));
File file = new File(imagePathList.get(index));
RequestBody imageBody = RequestBody.create(MediaType.parse("image/*"),
file);
surveyImagesParts[index] = MultipartBody.Part.createFormData("image",
file.getName(),
imageBody);
}
RequestBody event_date= RequestBody.create(MediaType.parse("text/plain"),
event_date_);
.
.
.
Call<LocationResponse> call = service.uploadImage(surveyImagesParts,event_date,....);
call.enqueue(new Callback<LocationResponse>() {
@Override
public void onResponse(Call<LocationResponse> call, Response<LocationResponse> response) {
if(response.body() != null)
{
displayAlert(MyApp.getContext().getString(R.string.notification_successful_text),MyApp.getContext().getString(R.string.upload_photo_successful_text));
} else {
}
}
@Override
public void onFailure(Call<LocationResponse> call, Throwable t) {
}
});
}
我这样用API;
@POST("api/info/save")
Call<ResponseBody> uploadMultiFile(@Body RequestBody file);
此函数从数组列表中获取图像并添加参数以发送服务器;
private void uploadMultiFile(final String event_date, final int event_type, final int coordinate_x, final int coordinate_y) {
String url = Constant.baseUrl;
Coordinate coordinate = new Coordinate();
coordinate.setX(coordinate_x);
coordinate.setY(coordinate_y);
TokenInterceptor interceptor=new TokenInterceptor();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(url)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Api service = retrofit.create(Api .class);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("event_date", event_date);
builder.addFormDataPart("event_type", String.valueOf(event_type));
builder.addFormDataPart("coordinate", String.valueOf(coordinate));
// Map is used to multipart the file using okhttp3.RequestBody
// Multiple Images
for (int i = 0; i < imagePathList.size(); i++) {
File file = new File(imagePathList.get(i));
builder.addFormDataPart("image[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
MultipartBody requestBody = builder.build();
Call<ResponseBody> call = service.uploadMultiFile(requestBody);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(MMEActivity.this, "Success " + response.message(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("d", "Error " + t.getMessage());
}
});
}
我遇到错误请求错误。我怎么解决这个问题?我在后端使用 Django。这是我从请求中获取图像和参数的函数。
@api_view(['POST'])
def eventInfoSave(request):
event_date = request.data['event_date']
print(request.data)
.
.
if bool(request.FILES.getlist('image', False)) == True:
uploaded_file = request.FILES.getlist('image')
当我使用邮递员时没有任何问题。我可以从一个键获取所有图像。
我使用解决了我的问题;
@Multipart
@POST("/save")
Call<Response> uploadImage(@Part MultipartBody.Part[] image, @Part("event_date") RequestBody event_date,
@Part("event_type") RequestBody event_type,
.
.
.);
我将我的功能更改为;
private void uploadMultiFile(final String event_date, final int event_type, final int coordinate_x, final int coordinate_y) {
String url = Constant.baseUrl;
Coordinate coordinate_ = new Coordinate();
coordinate_.setX(coordinate_x);
coordinate_.setY(coordinate_y);
String coordinate__ = new Gson().toJson(coordinate_);
TokenInterceptor interceptor=new TokenInterceptor();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(url)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Api service = retrofit.create(Api .class);
MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[imagePathList.size()];
for (int index = 0; index <
imagePathList.size(); index++) {
Log.d("ss",
"requestUploadSurvey: survey image " +
index +
" " +imagePathList.get(index));
File file = new File(imagePathList.get(index));
RequestBody imageBody = RequestBody.create(MediaType.parse("image/*"),
file);
surveyImagesParts[index] = MultipartBody.Part.createFormData("image",
file.getName(),
imageBody);
}
RequestBody event_date= RequestBody.create(MediaType.parse("text/plain"),
event_date_);
.
.
.
Call<LocationResponse> call = service.uploadImage(surveyImagesParts,event_date,....);
call.enqueue(new Callback<LocationResponse>() {
@Override
public void onResponse(Call<LocationResponse> call, Response<LocationResponse> response) {
if(response.body() != null)
{
displayAlert(MyApp.getContext().getString(R.string.notification_successful_text),MyApp.getContext().getString(R.string.upload_photo_successful_text));
} else {
}
}
@Override
public void onFailure(Call<LocationResponse> call, Throwable t) {
}
});
}