Android Studio:retrofit2:图像和字符串的多部分连接失败
Android Studio: retrofit2: multipart connection failure with image and strings
private void Dialog_profile_pic() {
// create upload service client
File file = new File(selectedImagePath);
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("memFile", file.getName(), requestFile);
// add another part within the multipart request
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), ApiResultCode.getApiKey());
RequestBody description2 =
RequestBody.create(
MediaType.parse("multipart/form-data"), ApiResultCode.getApiType());
Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
.loginPicture(description, description2, body);
loginPictureCall.enqueue(new Callback<LoginPicture>() {
@Override
public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
//CONNECTION SUCCESS
LoginPicture NewUser = response.body();
if (NewUser.getResponsedata().getResultCode() == 100) {
Log.e("DEBUG", "CONNECTION result: CONGRATS");
} else {
Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
}
}
@Override
public void onFailure(Call<LoginPicture> call, Throwable t) {
//CONNECTION FAIL
Log.e("DEBUG", "CONNECTION result: FAIL" );
}
});
}
\
public interface ApiInterface {
@Multipart
@POST("/memberController/joinUploadProfile.json")
Call<LoginPicture> loginPicture(@Part("apiKey") RequestBody apiKey, @Part("apiType") RequestBody apiType, @Part("memFile") MultipartBody.Part file); //multi part
\
我正在尝试使用 retrofit2 传递两个字符串参数和一个图像文件,但我无法建立连接。任何人都可以帮我找到我需要做什么?我已经为此苦苦挣扎了 2 天。
//请求客户端API接口
@Multipart
@POST("URL.json")
Call<LoginPicture> loginPicture(@PartMap() Map<String, RequestBody> mapPhoto); //multi part
//在activity
private void Dialog_profile_pic(final Uri selectedImageUri) {
// create upload service client
File file = new File(selectedImagePath);
HashMap<String, RequestBody> map = new HashMap<>();
RequestBody description =
RequestBody.create(
MediaType.parse("text/plain"),"content1");
RequestBody description2 =
RequestBody.create(
MediaType.parse("text/plain"), "content2");
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/jpeg"), file);
map.put("memFile\"; filename=\""+file.getName(),requestFile);
map.put("apiKey",description);
map.put("apiType", description2);
Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
.loginPicture(map);
loginPictureCall.enqueue(new Callback<LoginPicture>() {
@Override
public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
Picasso.with(Activity_create.this).load(NewUser.getResponsedata().getResultObject()).into(iv_profile_pic);
} else {
Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
}
}
@Override
public void onFailure(Call<LoginPicture> call, Throwable t) {
//통신 실패 시
Log.e("DEBUG", "CONNECTION result: FAIL");
}
});
}
//现在有效
private void Dialog_profile_pic() {
// create upload service client
File file = new File(selectedImagePath);
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("memFile", file.getName(), requestFile);
// add another part within the multipart request
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), ApiResultCode.getApiKey());
RequestBody description2 =
RequestBody.create(
MediaType.parse("multipart/form-data"), ApiResultCode.getApiType());
Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
.loginPicture(description, description2, body);
loginPictureCall.enqueue(new Callback<LoginPicture>() {
@Override
public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
//CONNECTION SUCCESS
LoginPicture NewUser = response.body();
if (NewUser.getResponsedata().getResultCode() == 100) {
Log.e("DEBUG", "CONNECTION result: CONGRATS");
} else {
Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
}
}
@Override
public void onFailure(Call<LoginPicture> call, Throwable t) {
//CONNECTION FAIL
Log.e("DEBUG", "CONNECTION result: FAIL" );
}
});
}
\
public interface ApiInterface {
@Multipart
@POST("/memberController/joinUploadProfile.json")
Call<LoginPicture> loginPicture(@Part("apiKey") RequestBody apiKey, @Part("apiType") RequestBody apiType, @Part("memFile") MultipartBody.Part file); //multi part
\ 我正在尝试使用 retrofit2 传递两个字符串参数和一个图像文件,但我无法建立连接。任何人都可以帮我找到我需要做什么?我已经为此苦苦挣扎了 2 天。
//请求客户端API接口
@Multipart
@POST("URL.json")
Call<LoginPicture> loginPicture(@PartMap() Map<String, RequestBody> mapPhoto); //multi part
//在activity
private void Dialog_profile_pic(final Uri selectedImageUri) {
// create upload service client
File file = new File(selectedImagePath);
HashMap<String, RequestBody> map = new HashMap<>();
RequestBody description =
RequestBody.create(
MediaType.parse("text/plain"),"content1");
RequestBody description2 =
RequestBody.create(
MediaType.parse("text/plain"), "content2");
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/jpeg"), file);
map.put("memFile\"; filename=\""+file.getName(),requestFile);
map.put("apiKey",description);
map.put("apiType", description2);
Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
.loginPicture(map);
loginPictureCall.enqueue(new Callback<LoginPicture>() {
@Override
public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
Picasso.with(Activity_create.this).load(NewUser.getResponsedata().getResultObject()).into(iv_profile_pic);
} else {
Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
}
}
@Override
public void onFailure(Call<LoginPicture> call, Throwable t) {
//통신 실패 시
Log.e("DEBUG", "CONNECTION result: FAIL");
}
});
}
//现在有效