无法从PHP中的Raturofit发送请求主体



我正在尝试使用Raturofit将多个图像发送到服务器我在做什么是发送请求界的地图,这是我的代码

  @Multipart
@POST("imageuload")
Call<ResponseBody> postImage(@PartMap Map<String, RequestBody> files );

和我的活动

   Map<String, RequestBody> filestosend = new HashMap<>();
                for (int pos = 0; pos < files.size(); pos++) {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), files.get(pos));
                    filestosend.put("photo_" + String.valueOf(pos + 1), requestBody);
                }


                Call<ResponseBody> call = apiSerice.postImage(filestosend);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        if (response.isSuccessful()) {
                            try {
                                Toast.makeText(getBaseContext(),response.body().string(),Toast.LENGTH_LONG).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }else {
                            try {
                                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                                alert.setMessage(response.errorBody().string());
                                alert.show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        t.printStackTrace();

                    }
                });

当我想测试我的AM收到的内容时,我的响应中没有得到空的响应。

echo file_get_contents('php://input');

我什至用单个请求正文进行了测试

  RequestBody test = RequestBody.create(MediaType.parse("text/plain"), "test");
                Call<ResponseBody> call = apiSerice.postImage(test);

但是我在回复中仍然有空的回应我将感谢任何帮助或评论

可以通过$_FILES访问PHP中的多部分上传。PHP手册对php://input有以下内容:

php://enctype =" multipart/form-data"。

无法获得输入。

一个完整的工作示例(减去正确的端点URL,硬编码字节数组):

public class Sample {
    interface SampleService {
        @Multipart
        @POST("/test.php")
        Call<ResponseBody> postImage(@Part List<MultipartBody.Part> files);
    }
    public static void main(String[] args) throws IOException {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://...").build();
        SampleService service = retrofit.create(SampleService.class);
        RequestBody file1 = RequestBody.create(MediaType.parse("image/jpeg"), new byte[]{0x00});
        MultipartBody.Part part1 = MultipartBody.Part.createFormData("A kitten", "Kitten.jpg", file1);
        RequestBody file2 = RequestBody.create(MediaType.parse("image/jpeg"), new byte[]{0x00});
        MultipartBody.Part part2 = MultipartBody.Part.createFormData("Another kitten", "Kitten2.jpg", file2);
        System.out.println(service.postImage(Arrays.asList(part1, part2)).execute().body().string());
    }
}

服务器代码:

<?php var_dump($_FILES); ?>

客户端输出:

array(2) {
  ["A_kitten"]=>
  array(5) {
    ["name"]=>
    string(10) "Kitten.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpml5PIP"
    ["error"]=>
    int(0)
    ["size"]=>
    int(1)
  }
  ["Another_kitten"]=>
  array(5) {
    ["name"]=>
    string(11) "Kitten2.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpzhgXm0"
    ["error"]=>
    int(0)
    ["size"]=>
    int(1)
  }
}

最新更新