【问题标题】:HttpPost returning error when using MultipartEntityBuilder in Android在 Android 中使用 MultipartEntityBuilder 时 HttpPost 返回错误
【发布时间】:2014-10-21 07:10:33
【问题描述】:

我正在尝试查询“http://www.idmypill.com/api/id/”api,我收到的 JSON 字符串是 {"results":[],"success":false,"errors":null} 这是我的服务处理程序类:

public String makeServiceCall(String url, int method, 
        String api, byte[] pillImage) 
{
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;


        // Checking http request method type
         if (method == POST) 
        {
            android.os.Debug.waitForDebugger();
            HttpPost httpPost = new HttpPost(url);

            httpPost.setHeader("data = api_key", api); 
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("files = image", pillImage); 
            entity = builder.build();
            Log.d("Entity", entity.toString()); 

            httpPost.setEntity(entity);
            Log.d("post", httpPost.toString()); 
            httpResponse = httpClient.execute(httpPost);

            Log.d("params", httpResponse.getParams().toString()); 

        } 

        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

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

    return response;

}

网站给出的python示例是:

# highly suggested to use the requests package
# http://www.python-requests.org/en/latest/
import requests

# read in the image and construct the payload
image = open("example.jpg").read()
data = {"api_key": "KH8hdoai0wrjB0LyeA3EMu5n4icwyOQo"}
files = {"image": open("example.jpg")}

# fire off the request
r = requests.post("http://www.idmypill.com/api/id/",
    data = data,
    files = files)

# contents will be returned as a JSON string
print r.content

不知何故,我的发布格式一定是错误的,或者他们是否可能特别想要 .jpg 图像而不是字节数组? 我不熟悉 Python,并且已经在这个问题上苦苦挣扎了一个多星期,所以任何帮助都将不胜感激。

【问题讨论】:

    标签: java python json http-post multipartentity


    【解决方案1】:

    试试:

     ...
     MultipartEntityBuilder builder = MultipartEntityBuilder.create();
     builder.addTextBody("api_key", api);
     builder.addPart("image", pillImage); 
     ...
    

    如果addPart 不适用于字节数组(我正在工作,无法测试),那么取图像文件的名称并这样做肯定会起作用:

     ...
     pillImage = "/path/to/the/image.jpg";  //This is the image file name
     MultipartEntityBuilder builder = MultipartEntityBuilder.create();
     builder.addTextBody("api_key", api);
     File imageFile = new File(pillImage);  //Open the image
     builder.addPart("image", imageFile); 
     ...
    

    【讨论】:

    • 当我尝试使用文件时仍然收到错误消息,因为它正在寻找我很难理解的 ContentBody。 MultipartEntityBuilder 类型中的方法 addPart(String, ContentBody) 不适用于参数 (String, File)
    【解决方案2】:

    builder.addPart("file", new FileBody(new File(filename)));

    试试这个,而不是只在 addPart 中使用文件对象

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      相关资源
      最近更新 更多