我正在使用Retrofit2,我使用@FormUrlEncoded通过它发送数据,因为所有内容甚至都是字符串,甚至是图像,但是当我将图像集合作为字符串发送时遇到了一些错误,所以我更改为多部分。
现在,我面临无法发送包含多部分字符串的@Field的问题。
那么,如何在不为每个字符串使用RequestBody的情况下解决此问题?
这是我的代码
@Multipart
@POST("/androidfiles/insertNews.php")
Call<ResponseBody> uploadImage(@Part MultipartBody.Part file, @Part("name") RequestBody name,
@Field("tArabic") String arabicTitle,
@Field("tEnglish") String englishTitle,
@Field("tRussian") String russianTitle,
@Field("tItalian") String italianTitle,
@Field("dArabic") String arabicDescription,
@Field("dEnglish") String englishDescription,
@Field("dRussian") String russianDescription,
@Field("dItalian") String italianDescription);
和这里。
private void insertNews(Uri uri){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ServiceConstants.URL)
.build();
File file = FileUtil.getFile(this, uri);
RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), mFile);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
ApiConfig apiConfig = retrofit.create(ApiConfig.class);
Call<ResponseBody> addNews = apiConfig.uploadImage(fileToUpload,
filename,
etArabicTitle.getText().toString(),
etEnglishTitle.getText().toString(),
etRussianTitle.getText().toString(),
etItalianTitle.getText().toString(),
etArabicDescription.getText().toString(),
etEnglishDescription.getText().toString(),
etRussianDescription.getText().toString(),
etItalianDescription.getText().toString());
addNews.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(News.this, "success" + "\n" + response.message(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(News.this,Menu.class);
startActivity(intent);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(News.this, "fail" + t.getMessage(), Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
});
}
答案 0 :(得分:1)
尝试将@Field
更改为@Part
。像这样:
@Multipart
@POST("/androidfiles/insertNews.php")
Call<ResponseBody> uploadImage(@Part MultipartBody.Part file, @Part("name") RequestBody name,
@Part("tArabic") String arabicTitle,
@Part("tEnglish") String englishTitle,
@Part("tRussian") String russianTitle,
@Part("tItalian") String italianTitle,
@Part("dArabic") String arabicDescription,
@Part("dEnglish") String englishDescription,
@Part("dRussian") String russianDescription,
@Part("dItalian") String italianDescription);
答案 1 :(得分:1)
在使用Retrofit的多部分请求时,可以使用带有@Part
字段的RequestBody
批注来传递其他参数。
因此,您可以将其作为RequestBody
类型的参数传递给除多部分主体之外的所有参数,如下所示:
@Multipart
@POST("/androidfiles/insertNews.php")
Call<ResponseBody> uploadImage(@Part MultipartBody.Part file,
@Part("name") RequestBody name,
@Part("tArabic") RequestBody arabicTitle,
@Part("tEnglish") RequestBody englishTitle,
@Part("tRussian") RequestBody russianTitle,
@Part("tItalian") RequestBody italianTitle,
@Part("dArabic") RequestBody arabicDescription,
@Part("dEnglish") RequestBody englishDescription,
@Part("dRussian") RequestBody russianDescription,
@Part("dItalian") RequestBody italianDescription);
现在,用于调用此类 API ;您需要通过使用语法RequestBody
text/plain
为附加参数创建(i.e. RequestBody.create(MediaType.parse("text/plain"), your variable goes here))
来传递它
在建立API期间,您可以通过以下方式调用它:
Call<ResponseBody> addNews = apiConfig.uploadImage(fileToUpload,
filename,
RequestBody.create(MediaType.parse("text/plain"), etArabicTitle.getText().toString()), //This will make it as RequestBody to pass it along image/files.
RequestBody.create(MediaType.parse("text/plain"), etEnglishTitle.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etRussianTitle.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etItalianTitle.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etArabicDescription.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etEnglishDescription.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etRussianDescription.getText().toString()),
RequestBody.create(MediaType.parse("text/plain"), etItalianDescription.getText().toString()));