6

我正在尝试通过我的 android 应用程序制作HttpPost一个multiPart/form-data。我有一个使用我的 api 的邮递员测试,并且在邮递员中预览该请求,如下所示:

POST /api/0.1/content/upload HTTP/1.1
Host: 54.221.194.167
X-AUTHORIZATION: 166e649911ff424eb14446cf398bd7d6
Cache-Control: no-cache
Postman-Token: 2412eba9-f72d-6f3b-b124-6070b5b26644

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"

{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"; filename="addedaslib.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C

我正在尝试将 multipart/form-data 与我的 android HttpPost 一起使用,但它似乎不起作用。有没有办法“预览”我的请求并查看它实际上是如何发布到 api 的?我究竟做错了什么?我的代码:

public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {


        client = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        HttpResponse response = null; 
        MultipartEntity mpe = new MultipartEntity();
    try {


        Log.v("API", "URL:"+url);
        request.setHeader("Content-Type", "multipart/form-data");
        request.addHeader("X-AUTHORIZATION",token);
        request.addHeader("Cache-Control", "no-cache");

        DRPContentForUpload content = new DRPContentForUpload(file);
        String jsonObject = DRPJSONConverter.toJson(content);

        FormBodyPart part1= new FormBodyPart("file01", new StringBody(jsonObject));
        FormBodyPart part2= new FormBodyPart("file01", new FileBody(file)); 
        mpe.addPart(part1);
        mpe.addPart(part2);

        //

        request.setEntity(mpe);
        Log.v("RAW REQUEST", "request looks like:"+mpe.toString());
        response = client.execute(request);

编辑

我能够与我的 API 团队交谈,他们说我的帖子实际上是这样的:

--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"; filename="IMG_20140131_111622.jpg"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

那就是说它仍然无法正常工作并吐出我缺少参数的错误

这是我项目中包含的库的屏幕截图:

在此处输入图像描述

4

2 回答 2

12

所以在高低寻找答案,几乎放弃之后,这个链接终于有帮助了

这是最终的工作代码:

 public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {

    client = new DefaultHttpClient();

    HttpPost request = new HttpPost(url);

    HttpResponse response = null;

    DRPContentForUpload content = new DRPContentForUpload(file);
    String jsonObject = DRPJSONConverter.toJson(content);
    String BOUNDARY= "--eriksboundry--";

    request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
    request.addHeader("X-AUTHORIZATION",token);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
    try {


        entity.addPart("file01", new StringBody(jsonObject));

        entity.addPart("file01", new FileBody(file));

        request.addHeader("Accept-Encoding", "gzip, deflate");

    } catch (UnsupportedEncodingException e) {
        Log.v("encoding exception","E::: "+e);
        e.printStackTrace();
    }
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
    request.setEntity(entity);

    try {




        response = client.execute(request);



    } catch (ClientProtocolException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


    return response;

}
于 2014-02-07T15:30:39.837 回答
0

问题是您的图像的 mime-type 设置不正确吗?尝试使用替代构造函数FileBody

FormBodyPart part2 = new FormBodyPart("file01", new FileBody(file, "image/jpeg"));

为多部分请求的两个部分赋予相同的名称有点可疑,但在这里似乎不会给您带来问题。

如果您想在发送之前查看您的身体是什么样子,请在添加所有元素后尝试在多部分实体上使用EntityUtils.toString(Entity) 。该图像只是二进制的,因此打印效果不佳,但标题应该是可读的。

于 2014-02-07T15:14:28.197 回答