我正在尝试将两个图像从我的Android设备的内部存储上传到HTTP服务器。
public interface ApiService {
...
@Multipart
@Headers("Content-Type: application/json")
@POST("signature/{id}")
Call<String> sendSignature(
@Header("Authorization") String authorization,
@Path("id") String id,
@Part("descrtipion") RequestBody description,
@Part MultipartBody.Part file1,
@Part MultipartBody.Part file2);
}
...
private void sendSignatures(){
RequestBody description = RequestBody.create(okhttp3.MultipartBody.FORM, getString(R.string.str_file_description));
File file2 = getFileStreamPath(SIGNATURE2_PATH);
RequestBody requestFile2 = RequestBody.create(MediaType.parse("image/png"), file2);
MultipartBody.Part body2 = MultipartBody.Part.createFormData("sign1", file2.getName(), requestFile2);
File file = getFileStreamPath(SIGNATURE_PATH);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("sign2", file.getName(), requestFile);
Call<String> call = ApiFactory.getService().sendSignature(token, PARAMETER_ID, description, body2, body);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) Log.d("myLogs", "Yes: " + response.body());
else Toast.makeText(MainActivity.this, ErrorUtils.errorMessage(response), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(MainActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});
}
但服务器返回错误#500。同时Postman的请求成功(#200)。
请帮我解决一下:我在哪里犯了java代码错误?
答案 0 :(得分:0)
我找到了解决方案。特别是在这种情况下,有必要删除标题:
public interface ApiService {
...
@Multipart
@POST("signature/{id}")
Call<String> sendSignature(
@Header("Authorization") String authorization,
@Path("id") String id,
@Part("descrtipion") RequestBody description,
@Part MultipartBody.Part file1,
@Part MultipartBody.Part file2);
}
当我尝试通过Postman发送此请求时,只放置一个文件或两个文件,其中一个或两个都有错误的密钥,即使令牌不正确,服务器也会传递#500错误。首先,他们检查密钥和文件,然后 - 授权参数。我删除了标题,在这种情况下得到了成功的回复。