HttpPost returning error when using MultipartEntityBuilder in Android
Asked Answered
L

2

1

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;

}

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

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.

Lehr answered 21/10, 2014 at 7:10 Comment(0)
F
0

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

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

Frogmouth answered 22/10, 2014 at 0:17 Comment(0)
M
1

Try:

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

If addPart doesn't work with a byte array (I'm at work, cant test), taking the name of the image file and doing this will definitely work:

 ...
 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); 
 ...
Marcellusmarcelo answered 21/10, 2014 at 8:24 Comment(1)
I still receive an error when I try to use a file because it is looking for a ContentBody which I am having a hard time understanding what that even is. The method addPart(String, ContentBody) in the type MultipartEntityBuilder is not applicable for the arguments (String, File)Lehr
F
0

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

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

Frogmouth answered 22/10, 2014 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.