在一个键中使用改造发送多个图像
Sending multiple images using retrofit in a single key
我已经尝试了互联网上所有可用的方法,但对我来说没有任何效果。
我正在尝试以单个键值
发送图像
{
files[]
}
这是我的界面
public interface UserClient {
@Multipart
@POST(".")
Call<JsonElement> upload(
@Part("text") RequestBody text,
@Part("image") RequestBody image,
@Part("login") RequestBody login,
@Part List<MultipartBody.Part> files
);
}
这是我的上传方法
public void uploadFiles(String text) {
QBUser user = SharedPrefsHelper.getInstance().getQbUser();
String userName = user.getFullName();
String image = "https://image.flaticon.com/icons/svg/146/146031.svg";
String URL = URLs.URL_POST + user.getPhone() + "/";
RequestBody textPart = RequestBody.create(MultipartBody.FORM, text);
RequestBody imagePart = RequestBody.create(MultipartBody.FORM, image);
RequestBody loginPart = RequestBody.create(MultipartBody.FORM, userName);
//list of files
List<MultipartBody.Part> filesList = new ArrayList<>();
for (int i = 0; i < arrayList_FilePath.size(); i++) {
filesList.add(prepareFilePart("files" + i, arrayList_FilePath.get(i)));
}
//Create retrofit instance
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
UserClient client = retrofit.create(UserClient.class);
Log.i(TAG, "uploadFiles: " + filesList);
Call<JsonElement> call = client.upload(textPart, imagePart, loginPart, filesList);
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
finish();
Toast.makeText(getApplicationContext(), "Successful: " + response.message(),
Toast.LENGTH_LONG).show();
Log.i(TAG, "onResponse: " + response.body() + response.message());
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_LONG).show();
}
});
}
这是preparefilePart方法
public MultipartBody.Part prepareFilePart(String partName, String path){
long imagename = System.currentTimeMillis();
File file = new File(path);
RequestBody requestBody = RequestBody.create(
MediaType.parse("image/*"),
file
);
return MultipartBody.Part.createFormData(partName, imagename + ".jpeg" ,requestBody);
}
如果我只发送没有图像的文本请求,它工作正常,但是当我发送图像时它不起作用
它给出了一个错误代码 500 内部服务器错误,正文中有空响应
注意:Api 在 Postman 中运行良好
文件未上传,因为文件未正确创建
感谢 github 我找到了正确创建文件的方法
也根据这个答案对我的代码做了一些修改
我已经尝试了互联网上所有可用的方法,但对我来说没有任何效果。 我正在尝试以单个键值
发送图像{
files[]
}
这是我的界面
public interface UserClient {
@Multipart
@POST(".")
Call<JsonElement> upload(
@Part("text") RequestBody text,
@Part("image") RequestBody image,
@Part("login") RequestBody login,
@Part List<MultipartBody.Part> files
);
}
这是我的上传方法
public void uploadFiles(String text) {
QBUser user = SharedPrefsHelper.getInstance().getQbUser();
String userName = user.getFullName();
String image = "https://image.flaticon.com/icons/svg/146/146031.svg";
String URL = URLs.URL_POST + user.getPhone() + "/";
RequestBody textPart = RequestBody.create(MultipartBody.FORM, text);
RequestBody imagePart = RequestBody.create(MultipartBody.FORM, image);
RequestBody loginPart = RequestBody.create(MultipartBody.FORM, userName);
//list of files
List<MultipartBody.Part> filesList = new ArrayList<>();
for (int i = 0; i < arrayList_FilePath.size(); i++) {
filesList.add(prepareFilePart("files" + i, arrayList_FilePath.get(i)));
}
//Create retrofit instance
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
UserClient client = retrofit.create(UserClient.class);
Log.i(TAG, "uploadFiles: " + filesList);
Call<JsonElement> call = client.upload(textPart, imagePart, loginPart, filesList);
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
finish();
Toast.makeText(getApplicationContext(), "Successful: " + response.message(),
Toast.LENGTH_LONG).show();
Log.i(TAG, "onResponse: " + response.body() + response.message());
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_LONG).show();
}
});
}
这是preparefilePart方法
public MultipartBody.Part prepareFilePart(String partName, String path){
long imagename = System.currentTimeMillis();
File file = new File(path);
RequestBody requestBody = RequestBody.create(
MediaType.parse("image/*"),
file
);
return MultipartBody.Part.createFormData(partName, imagename + ".jpeg" ,requestBody);
}
如果我只发送没有图像的文本请求,它工作正常,但是当我发送图像时它不起作用 它给出了一个错误代码 500 内部服务器错误,正文中有空响应
注意:Api 在 Postman 中运行良好
文件未上传,因为文件未正确创建 感谢 github 我找到了正确创建文件的方法
也根据这个答案对我的代码做了一些修改