Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Multipart HTTP Post does not send the File's MIME type

Trying to figure what's wrong with my codings. I followed a blog post from here.

I managed to get the codes to actually upload the file to a PHP web service. However, for some reason although I've set explicitly the MIME type for the file, PHP shows that the MIME is just a blank string and therefore rejected.

Here's my codings:

public String SendPost(String fn, String bid, String caption, String uid, String APIKey, String postHash) 
        throws ParseException, ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost(UrbURL);

    Log.i("POSTFN", fn);
    Log.i("POSTFN", bid);
    Log.i("POSTFN", caption);
    Log.i("POSTFN", uid);
    Log.i("POSTFN", APIKey);
    Log.i("POSTFN", postHash);

    String postAuth = uid + postHash;
    postAuth = md5(postAuth);
    postAuth = postAuth.substring(0, 16);
    //Log.i("POSTAUTH", postAuth);

    MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    /*File tempImg = new File(fn);
    FileBody bin = new FileBody(tempImg, "image/jpg");*/
    mp.addPart("business_photo", new FileBody(new File(fn), "image/jpg"));

    //StringBody s = new StringBody(bid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("business_id", new StringBody(bid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(caption, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("photo_caption", new StringBody(caption, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(uid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("user_id", new StringBody(uid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("apikey", new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("auth", new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" )));

    httppost.setEntity(mp);

    String response = EntityUtils.toString( httpclient.execute( httppost ).getEntity(), "UTF-8" );

    httpclient.getConnectionManager().shutdown();

    return response;
}

Many thanks before :)

like image 270
Tista Avatar asked Aug 14 '10 09:08

Tista


People also ask

What is the MIME type for multipart form data?

The multipart/form-data content type is intended to allow information providers to express file upload requests uniformly, and to provide a MIME-compatible representation for file upload responses. The multipart/mixed content type is used when the body parts are independent and need to be bundled in a particular order.

What is multipart request in Android?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).


2 Answers

I had the same problem and just fixed it.

I found that using the HttpMultipartMode.BROWSER_COMPATIBLE prevented the correct mimeType from being set in my request, when using a ByteArrayBody for an image. I assume this is probably the same problem you are having.

When I changed this line:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

to

MultipartEntity mp = new MultipartEntity();

then the mime type was set correctly and the service upload worked.

I see that people used the BROWSER_COMPATIBLE for solving another problem, but hopefully you don't need it.

like image 155
Ryan Avatar answered Oct 04 '22 21:10

Ryan


I just had the same issue when updating HttpClient from version 4.1 to version 4.4. Neither removing HttpMultipartMode.BROWSER_COMPATIBLE nor replacing with other alternative options did help.

A solution that requires the minimum change for me is to explicitly specify the filename when constructing a FileBody, i.e. replacing

new FileBody(file, mimeType)

with

new FileBody(file, filename, mimeType, charset)

As for version HttpClient 4.4, the reason might be that new FileBody(file, mimeType) assigns null to the filename field rather than uses file.getName(). Then, when executing formatMultipartHeader in HttpMultipart, the filename of the part that is going to be formatted is checked if not null in BROWSER_COMPATIBLE mode. If null, then the whole body is ignored.

like image 26
yangtseyangtse Avatar answered Oct 04 '22 20:10

yangtseyangtse