在Android中使用MultipartEntityBuilder时,HttpPost返回错误 [英] HttpPost returning error when using MultipartEntityBuilder in Android

查看:157
本文介绍了在Android中使用MultipartEntityBuilder时,HttpPost返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to query "http://www.idmypill.com/api/id/" api and the JSON string I am receiving back is {"results":[],"success":false,"errors":null} This is my service handler class:

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示例是:

The python example the website gives is:

# 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,并且已经在这个问题上苦苦挣扎了一个多星期了,所以任何帮助都会非常感激。

Somehow my format for posting must be wrong or is it possible they specifically want a .jpg image instead of a byte array? I am not familiar with Python and have been struggling with this problem for over a week now so any help would be much appreciated.

推荐答案

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

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

试试这个,而不是仅仅使用addPart中的文件对象

Try this instead of just using a file object with in addPart

这篇关于在Android中使用MultipartEntityBuilder时,HttpPost返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆