【问题标题】:cant get request body sent from retrofit in php无法获取从 php 中的改造发送的请求正文
【发布时间】:2017-07-18 09:41:55
【问题描述】:

我正在尝试使用改造将多个图像发送到服务器 我正在做的是发送 RequestBody 的地图,这是我的代码

  @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();


                    }
                });

当我想测试我得到什么时,它会从服务器返回一个空的响应,而我的响应中什么也没得到..

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

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

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

                Call<ResponseBody> call = apiSerice.postImage(test);

但我在回复中仍然得到一个空的回复 我将不胜感激任何帮助或评论

【问题讨论】:

    标签: php android retrofit


    【解决方案1】:

    可以通过$_FILES 在 PHP 中访问分段上传。 PHP manual 对php://input 有以下说法:

    php://input 不适用于 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)
      }
    }
    

    【讨论】:

    • 但是如果想用 $_POST 得到它,得到它的关键是什么?
    • php://input 不适用于 enctype="multipart/form-data"。那我应该改变我的enctype吗?如果是的话怎么可能?
    • 对不起。我这边的错误(我的 PHP 非常生锈)。 $_POST 仅适用于 application/x-www-form-urlencoded 或 multipart/form-data。使用$_FILES 访问文件。
    • 哦,我真的很感谢你。如果我不能给你+1,现在我可以处理它了,谢谢你
    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多