Android 使用第2页发送图像
我正在尝试使用Reformation2将图像上载到服务器,但我不确定如何执行此操作 文档让我有点困惑,我尝试了上面提到的解决方案,但它对我不起作用 以下是我当前使用的代码段,它不会向服务器发送任何内容:Android 使用第2页发送图像,android,retrofit2,Android,Retrofit2,我正在尝试使用Reformation2将图像上载到服务器,但我不确定如何执行此操作 文档让我有点困惑,我尝试了上面提到的解决方案,但它对我不起作用 以下是我当前使用的代码段,它不会向服务器发送任何内容: // Service @Multipart @POST("0.1/gallery/{galleryId}/addImage/") Call<ResponseBody> addImage(@Path("galleryId") String galleryId, @Part Multip
// Service
@Multipart
@POST("0.1/gallery/{galleryId}/addImage/")
Call<ResponseBody> addImage(@Path("galleryId") String galleryId, @Part MultipartBody.Part image);
//Call
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<ResponseBody> call = service. addImage("1234567890", imagePart);
//服务
@多部分
@POST(“0.1/gallery/{galleryId}/addImage/”)
调用addImage(@Path(“galleryId”)字符串galleryId,@Part MultipartBody.Part image);
//召唤
MultipartBody.Part imagePart=MultipartBody.Part.createFormData(“image”,file.getName(),RequestBody.create(MediaType.parse(“image/*”,file));
呼叫=服务。addImage(“1234567890”,imagePart);
不过,我可以通过使用TypedFile对1.9进行改装来完成这项工作
我是做错了什么,还是改型2在这类事情上有问题?我为此奋斗了一段时间,最终我找到了这个解决方案,使它能够工作。。。希望它有助于:
Map<String, RequestBody> map = new HashMap<>();
map.put("Id",Utils.toRequestBody("0"));
map.put("Name",Utils.toRequestBody("example"));
String types = path.substring((path.length() - 3), (path.length()));
File file = new File(pathOfYourFile);
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
map.put("file\"; filename=\"cobalt." + types + "\"", fileBody);
Call<ResponseBody> call = greenServices.upload(map);
您好,请检查我们如何在2发送图像。在部分的形式,您需要发送图像和其他数据以及
public interface ApiInterface {
@Multipart
@POST("0.1/gallery/{galleryId}/addImage/")
Call<User> checkapi (@Part("file\"; filename=\"pp.png\" ") RequestBody file , @Part("FirstName") RequestBody fname, @Part("Id") RequestBody id);
}
File file = new File(imageUri.getPath());
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), firstNameField.getText().toString());
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), AZUtils.getUserId(this));
Call<User> call = client.editUser(AZUtils.getToken(this), fbody, name, id);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(retrofit.Response<User> response, Retrofit retrofit) {
AZUtils.printObject(response.body());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
公共接口{
@多部分
@POST(“0.1/gallery/{galleryId}/addImage/”)
调用checkapi(@Part(“file\”文件名=\“pp.png\”)RequestBody文件、@Part(“FirstName”)RequestBody fname、@Part(“Id”)RequestBody Id);
}
File File=新文件(imageUri.getPath());
RequestBody fbody=RequestBody.create(MediaType.parse(“image/*”),文件);
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”)、firstNameField.getText().toString();
RequestBody id=RequestBody.create(MediaType.parse(“text/plain”)、AZUtils.getUserId(this));
Call Call=client.editUser(AZUtils.getToken(this),fbody,name,id);
call.enqueue(新回调(){
@凌驾
公共响应(改装。响应,改装){
printObject(response.body());
}
@凌驾
失效时的公共无效(可丢弃的t){
t、 printStackTrace();
}
});
谢谢,希望这能对您有所帮助。尝试使用
“多部分/表单数据”
而不是“image/*”
您尝试过该解决方案吗?可能会有帮助您最终能够做到吗?如果没有,请告诉我
public interface ApiInterface {
@Multipart
@POST("0.1/gallery/{galleryId}/addImage/")
Call<User> checkapi (@Part("file\"; filename=\"pp.png\" ") RequestBody file , @Part("FirstName") RequestBody fname, @Part("Id") RequestBody id);
}
File file = new File(imageUri.getPath());
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), firstNameField.getText().toString());
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), AZUtils.getUserId(this));
Call<User> call = client.editUser(AZUtils.getToken(this), fbody, name, id);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(retrofit.Response<User> response, Retrofit retrofit) {
AZUtils.printObject(response.body());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});