Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 多部件改装1.9->;2._Android_Retrofit_Retrofit2_Okhttp3 - Fatal编程技术网

Android 多部件改装1.9->;2.

Android 多部件改装1.9->;2.,android,retrofit,retrofit2,okhttp3,Android,Retrofit,Retrofit2,Okhttp3,我曾经用改装1.9发送POST请求: TypedFile typedFile = picture != null ? new TypedFile("image/*", tempFile) : null; @Multipart @POST("/goals") Observable<Goal> postGoal( @Part("name") String name, @Part("picture") TypedFile picture

我曾经用改装1.9发送POST请求:

TypedFile typedFile =  picture != null ? new TypedFile("image/*", tempFile) : null;

@Multipart
@POST("/goals")
Observable<Goal> postGoal(
        @Part("name") String name,
        @Part("picture") TypedFile picture
        );
TypedFile TypedFile=图片!=无效的新类型文件(“image/*”,tempFile):null;
@多部分
@职位(“/目标”)
可观测后目标(
@部分(“名称”)字符串名称,
@零件(“图片”)类型文件图片
);
我改用了改装版2.0.2,但我无法让它工作相同。根据我使用的:

RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),name);
RequestBody picture=RequestBody.create(MediaType.parse(“image/*”),tempFile);
@多部分
@职位(“目标”)
可观测后目标(
@部分(“名称”)请求主体名称,
@部分(“picture\”文件名=\“temp.png\”)请求正文图片
);

但请求似乎没有文件(照片)。怎么了?

要使用改型2.0上载图像,您可以使用MultipartBody.Part作为图像的参数。希望此解决方案解决您的问题

您的API声明

@Multipart
@POST("/goals")
Observable<Goal> postGoal(@Part("name") String name, @Part MultipartBody.Part imageFile);
@Multipart
@职位(“/目标”)
可观察的postGoal(@Part(“name”)字符串名,@Part MultipartBody.Part imageFile);
您的api调用

RequestBody imageName = RequestBody.create(MediaType.parse("text/plain"), name);

//prepare image file
File file = new File(imagePath);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);

YourAPI service = retrofit.create(YourAPI.class);
Call<Goal> call = service.postGoal(imageName, imageFileBody);
call.enqueue(new Callback<Goal>() {
    @Override
    public void onResponse(Call<Goal> call, Response<Goal> response) {
        //handle success
    }

    @Override
    public void onFailure(Call<Goal> call, Throwable t) {

    }
});
RequestBody-imageName=RequestBody.create(MediaType.parse(“text/plain”),name);
//准备图像文件
文件文件=新文件(imagePath);
RequestBody RequestBody=RequestBody.create(MediaType.parse(“多部分/表单数据”),文件);
MultipartBody.Part imageFileBody=MultipartBody.Part.createFormData(“picture”,file.getName(),requestBody);
YourAPI service=reformation.create(YourAPI.class);
Call Call=service.postGoal(imageName,imageFileBody);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//成功
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});

要使用改型2.0上传图像,您可以使用MultipartBody.Part作为图像的参数。希望此解决方案能够解决您的问题

您的API声明

@Multipart
@POST("/goals")
Observable<Goal> postGoal(@Part("name") String name, @Part MultipartBody.Part imageFile);
@Multipart
@职位(“/目标”)
可观察的postGoal(@Part(“name”)字符串名,@Part MultipartBody.Part imageFile);
您的api调用

RequestBody imageName = RequestBody.create(MediaType.parse("text/plain"), name);

//prepare image file
File file = new File(imagePath);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);

YourAPI service = retrofit.create(YourAPI.class);
Call<Goal> call = service.postGoal(imageName, imageFileBody);
call.enqueue(new Callback<Goal>() {
    @Override
    public void onResponse(Call<Goal> call, Response<Goal> response) {
        //handle success
    }

    @Override
    public void onFailure(Call<Goal> call, Throwable t) {

    }
});
RequestBody-imageName=RequestBody.create(MediaType.parse(“text/plain”),name);
//准备图像文件
文件文件=新文件(imagePath);
RequestBody RequestBody=RequestBody.create(MediaType.parse(“多部分/表单数据”),文件);
MultipartBody.Part imageFileBody=MultipartBody.Part.createFormData(“picture”,file.getName(),requestBody);
YourAPI service=reformation.create(YourAPI.class);
Call Call=service.postGoal(imageName,imageFileBody);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//成功
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});

是的,这也正是我发现的。我希望会有更清晰的解决方案;)Thx!是的,这也正是我发现的。我希望会有更清晰的解决方案;)Thx!