我需要使用 Retrofit 2 向 API 发送请求。 我希望请求看起来像 Postman 中的表单数据。
我有一个 ArrayList 和一个要上传的图像。 所以我创建了这个类“CreateGroupCard”
public class CreateGroupCard implements Serializable
{
@SerializedName("userID")
String userID;
@SerializedName("group_name")
String group_name;
@SerializedName("new_name")
String new_name;
@SerializedName("group_id")
int group_id;
@SerializedName("group_members")
List<sendMember> group_members;
RequestBody photo;
public CreateGroupCard() {
}
private CreateGroupCard(String userID, String group_name, List<sendMember> group_members, RequestBody photo) {
this.userID = userID;
this.group_name = group_name;
this.group_members = group_members;
this.photo = photo;
}
}
这是界面
@POST(createGroupCard)
Call<CreatGroupCardApiResponse> createGroupCard
(@Header("Authorization") String authorization, @Body CreateGroupCard CreateGroupCard);
这是我的 API 调用
File file = new File(getRealPathFromURI(card.getPhotoUri()));
RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("photo",file.getName(),fileReqBody)
.build();
CreateGroupCard groupCard = new CreateGroupCard(user_id,card.getName(),members,requestBody);
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<CreatGroupCardApiResponse> call = apiInterface.createGroupCard("Bearer " + ACCESS_TOKEN,groupCard);
getRealPathFromURI 方法
public String getRealPathFromURI(Uri contentUri)
{
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
}
拍照并获取图像
private void openCamera()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, TAKE_PICTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
ImageView imageView = (ImageView)
viewPager.findViewWithTag("groupPhoto"+viewPager.getCurrentItem());
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
Uri uri = getImageUri(getApplicationContext(), imageBitmap);
Log.e("URI",uri.toString());
groupCards.get(viewPager.getCurrentItem()).setPhotoUri(uri);
}
}
但是图片没有上传,我做错了什么。
最佳答案
从 postman 那里我可以看到您正在将数据作为表单数据发送,因此您可以这样做:
@POST("<your endurl>")
Call<CreatGroupCardApiResponse> createGroupCard(@Header("Authorization") String authorization,@PartMap HashMap<String, RequestBody> partmap,@Part MultipartBody.Part part);
创建 RequestBody 类型的 HashMap :
HashMap<String, RequestBody> partBodyMap = new HashMap()
关于java - Retrofit 2 无法上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59623836/