使用OKhttp上传表单数据中的图像和音频,给我BadRequest Status 400

时间:2018-03-27 09:26:05

标签: android multipartform-data okhttp

我使用put请求上传图片并使用Okhttp服务器,请求与邮递员一起正常工作,但是通过android它给了我错误的请求 我已经实现了这里讨论的方法https://stackoverflow.com/a/23784452/9145387

请帮助我排序这个....先谢谢

getCollAsync = () => {
    return Promise.combine({
        dbase: ({ dbURL }) => { return mongoClient.connect(dbURL); },
        collInfos: ({ dbase }) => {
            return dbase.db(this.databaseName)
                .listCollections().toArray();
        },
    }, { dbURL: this.URL }).then(({ dbase, collInfos }) => {
        return Promise.resolve(collInfos.map((info) => {
            return dbase.collection(info.name);
        }));
    }).then((collections) => {
        return Promise.all(collections.map((collection) => {
            return collection.find({}).toArray();
        }))
    });
}

screenshot of the successful upload of image on postman

2 个答案:

答案 0 :(得分:0)

尝试此操作以重置超时:

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(60, TimeUnit.SECONDS); 
client.setReadTimeout(60, TimeUnit.SECONDS);

答案 1 :(得分:0)

错误在请求正文中。

   RequestBody requestBody = new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("image/jpg"), file))
            .addFormDataPart(caseId+"_audio", caseId+"_audio",
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();

更改了此行

 .addFormDataPart(caseId+"_audio", caseId+"_audio",
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();

   .addFormDataPart(caseId+"_audio", Audio.getName(),
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();