我正在尝试通过改造发送一个图像和一些字符串,但它不起作用,我总是得到400.我的应用程序已经使用GET或POST等Web服务而没有麻烦,我认为我的问题是上传...
我使用了explanations和改造的github。
我的想法是创建一个新的Dish并将其发送到我的服务器,因此大部分数据来自InputTextField,照片由相机使用Intent拍摄
首先我的上传界面
@Multipart
@POST("japronto/api/chef/dish/new")
Call<Dish> upload(@Part("name") RequestBody name, @Part("description") RequestBody description,@Part("disponibility") RequestBody disp,@Part("max") RequestBody max,@Part("price") RequestBody price, @Part MultipartBody.Part file);
我的上传功能
public void onOK(){
RequestBody name = RequestBody.create( MediaType.parse("multipart/form-data"), this.name.getText().toString());
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), this.description.getText().toString());
RequestBody disp =
RequestBody.create(
MediaType.parse("multipart/form-data"), Integer.toString(this.TorF));
RequestBody max =
RequestBody.create(
MediaType.parse("multipart/form-data"), this.max.getText().toString());
RequestBody price =
RequestBody.create(
MediaType.parse("multipart/form-data"), this.price.getText().toString());
File nImg = new File(folderImg, imgName);
RequestBody rqFile =
RequestBody.create(MediaType.parse("multipart/form-data"), nImg);
MultipartBody.Part body =
MultipartBody.Part.createFormData("picture", nImg.getName(), rqFile);
ApiService apiService = ApiManager.createService(ApiService.class, this.chef.getPseudo(), this.chef.getPassword());
Call<Dish> call = apiService.upload(name, description, disp, max, price, body);
call.enqueue(new Callback<Dish>() {
@Override
public void onResponse(Call<Dish> call, Response<Dish> response) {
Dish d = response.body();
Log.d(TAG, "onResponse: "+d.getName());
}
@Override
public void onFailure(Call<Dish> call, Throwable t) {
}
});
}
我在服务器上的视图
@app.route('/japronto/api/chef/dish/new', methods=['POST'])
def upload():
if request.method == 'POST':
if 'file' not in request.files:
print 'pbs'
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return {'name':filename}
return{'name':'erro'}
有什么想法吗?
答案 0 :(得分:0)
试试这个
@Multipart
@POST("japronto/api/chef/dish/new")
Call<Dish> upload(@Part("name") RequestBody name, @Part("description") RequestBody description,@Part("disponibility") RequestBody disp,@Part("max") RequestBody max,@Part("price") RequestBody price, @Part("ImageNameHere\"; filename=\"image.png\" ") RequestBody image);
File nImg = new File(folderImg, imgName);
RequestBody rqFile = RequestBody.create(MediaType.parse("image/png"), nImg);
检查这些链接