Search code examples
androidhttpfile-uploadmultipart

Post files to server from Android - MultipartEntityBuilder - HTTP


I am trying to upload some files to my server (via HTTP POST) from an Android app and I have checked many Questions here but can't get it to work. I hope someone can help me out:

I also included some variables in the URL to verify that at least those are reaching the server(GET).

    HttpClient httpClient = new DefaultHttpClient();
    String url="http://XXXXX.com/files/upload.php?flies=yes&eats=no&friend=yes";
    HttpPost httppost = new HttpPost(url);
    httppost.addHeader("Content-Type", "multipart/form-data");
    String content,response="";
    try {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addTextBody("randomvar", "42");

        //add image file
        //String filename="eye.png";
        //InputStream is=mContext.getAssets().open(filename);
        //builder.addBinaryBody("image", is, ContentType.create("image/png"), filename);

        //add text XML file
        final File file = new File(mContext.getFilesDir() +"/"+"serverXML.xml");
        FileBody fb = new FileBody(file);
        builder.addPart("file", fb);
        httppost.setEntity(builder.build());


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


        Log.i(TAG, "RESPONSE FROM SERVER: "+response);
    } catch (IOException e) {
        Log.i(TAG, "Exception: "+response);
        e.printStackTrace();
    }

This does run and get a response from the server. The following is the vardump of $_GET, $_POST, $_FILES:

enter image description here

I would want POST to have the file and variable

In my Eclipse Logcat I see the following after using MultiPartEntityBuilder:

10-06 02:36:57.231: D/dalvikvm(15888): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueParser;.INSTANCE
10-06 02:36:57.241: W/dalvikvm(15888): VFY: unable to resolve static field 5966 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
10-06 02:36:57.241: D/dalvikvm(15888): VFY: replacing opcode 0x62 at 0x001b
10-06 02:36:57.241: D/dalvikvm(15888): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueFormatter;.INSTANCE
10-06 02:36:57.241: W/dalvikvm(15888): VFY: unable to resolve static field 5960 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter;

I think I have correctly included all the required libraries:

enter image description here

EDIT:

I was able to solve this, my solution is the described in my answer below


Solution

  • I was fortunate enough to find one Answer by Krystian in Stackoverflow that solved my issue.

    After adding the boundary it started working. I had to add it to the HTTP Post

    String boundary = "-------------" + System.currentTimeMillis();
    httppost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
    

    and to the entity

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.setBoundary(boundary);
    

    Now I get everything in the server:

    10-06 13:16:06.977: I/UploadFileAsync(31506): RESPONSE FROM SERVER: Array
    10-06 13:16:06.977: I/UploadFileAsync(31506): (
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [flies] => yes
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [eats] => no
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [friend] => yes
    10-06 13:16:06.977: I/UploadFileAsync(31506): )
    10-06 13:16:06.977: I/UploadFileAsync(31506): Array
    10-06 13:16:06.977: I/UploadFileAsync(31506): (
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [randomvar] => 42
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [mystr] => blaka
    10-06 13:16:06.977: I/UploadFileAsync(31506): )
    10-06 13:16:06.977: I/UploadFileAsync(31506): Array
    10-06 13:16:06.977: I/UploadFileAsync(31506): (
    10-06 13:16:06.977: I/UploadFileAsync(31506):     [file] => Array
    10-06 13:16:06.977: I/UploadFileAsync(31506):         (
    10-06 13:16:06.977: I/UploadFileAsync(31506):             [name] => serverXML.xml
    10-06 13:16:06.977: I/UploadFileAsync(31506):             [type] => application/octet-stream
    10-06 13:16:06.977: I/UploadFileAsync(31506):             [tmp_name] => /tmp/phpdmEsn2
    10-06 13:16:06.977: I/UploadFileAsync(31506):             [error] => 0
    10-06 13:16:06.977: I/UploadFileAsync(31506):             [size] => 3548
    10-06 13:16:06.977: I/UploadFileAsync(31506):         )
    10-06 13:16:06.977: I/UploadFileAsync(31506): )