英文:
why cant upload image using multipart?
问题
我在使用多部分(Multipart)上传图像到服务器时遇到问题。当将图像上传到服务器时,响应显示成功,但响应内容为空,且未上传任何内容。但当我尝试发送不使用多部分的数据,如发送电子邮件或名称数据,发送成功。但在同一个 API 中,当我尝试使用多部分发送任何内容时,服务器未收到任何内容。
@Multipart
@Headers({
"Content-Type: application/json",
"X-Requested-With: XMLHttpRequest"})
@POST("profile")
Call<ResponseBody> upload(
@Header("Authorization") String authorization,
@Part MultipartBody.Part image
);
void data_1(File file) {
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
Retrofit retrofits = new Retrofit.Builder().baseUrl(Base_url).
addConverterFactory(GsonConverterFactory.create()).build();
networkis networkis = retrofits.create(www.gift_vouchers.com.NetworkLayer.networkis.class);
Call<ResponseBody> call = networkis.upload("Bearer " + new saved_data().get_token(UserInfoModelViewFactory.context),
body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
new utils().dismiss_dialog(UserInfoModelViewFactory.context);
Log.e("response_is", "" + response.message() + "dcd" + response.isSuccessful());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
new utils().dismiss_dialog(UserInfoModelViewFactory.context);
Log.e("error", t.getMessage());
}
});
}
英文:
i have problem with uploading image to server using multipart when uploading the image to sever the response is success but it get with null response and nothing uploaded when i trying to send data without using multipart send email or name data sent successfully but when i try to sent anything using part nothing sent to the server in the same api
@Multipart
@Headers({
"Content-Type: application/json",
"X-Requested-With: XMLHttpRequest"})
@POST("profile")
Call<ResponseBody> upload(
@Header("Authorization") String authorization,
@Part MultipartBody.Part image
);
void data_1(File file) {
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
Retrofit retrofits = new Retrofit.Builder().baseUrl(Base_url).
addConverterFactory(GsonConverterFactory.create()).build();
networkis networkis = retrofits.create(www.gift_vouchers.com.NetworkLayer.networkis.class);
Call<ResponseBody> call = networkis.upload("Bearer " + new saved_data().get_token(UserInfoModelViewFactory.context)
, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
new utils().dismiss_dialog(UserInfoModelViewFactory.context);
Log.e("response_is", "" + response.message() + "dcd" + response.isSuccessful());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
new utils().dismiss_dialog(UserInfoModelViewFactory.context);
Log.e("error", t.getMessage());
}
});
}
专注分享java语言的经验与见解,让所有开发者获益!
评论