我将捕获的图像转换为字节数组,并尝试将对象(包括字节数组)发布到Retrofit
中的服务器上,但是没有用。这是我的代码,谢谢。
Photo_List.java
public class Photo_List {
int FileId;
byte[] Picture;
String User;
public Photo_List(int FileId, byte[] Picture, String User) {
this.FileId=FileId;
this.User=User;
this.Picture=Picture;
}
public int getFileId() {
return FileId;
}
public byte[] getPicture() {
return Picture;
}
public String getUser() {
return User;
}
}
-我的方法-
private void multipartImageUpload() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 70, bos);
byte[] bitmapdata = bos.toByteArray();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("MYURL")
.addConverterFactory(GsonConverterFactory.create())
.build();
Photo_List item = new Photo_List(5, bitmapdata, "michonne");
ApiService apiService = retrofit.create(ApiService.class);
Call<Photo_List> photo_listCall = apiService.postImage(item);
photo_listCall.enqueue(new Callback<Photo_List>() {
@Override
public void onResponse(Call<Photo_List> call, Response<Photo_List> response) {
Log.d("GG", response.message());
Toast.makeText(getApplicationContext(), " :) ", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Photo_List> call, Throwable t) {
Log.e("WP", t.getMessage());
Toast.makeText(getApplicationContext(), ":(", Toast.LENGTH_LONG).show();
}
});}
ApiService.java
public interface ApiService {
@POST("api/r_photo")
Call<Photo_List> postImage(@Body Photo_List items);
}
POST api /
公共HttpResponseMessage帖子(照片)