MultipartEntity POST android

lonelyboy0212 picture lonelyboy0212 · Nov 2, 2013 · Viewed 17.6k times · Source

I'm implementing app upload file by android.I have a service : http://example.com:1001/UPLOAD/FileUpload.do. I want to upload a file and two parameter like that

0?event=Upload&type=:1

:0 SERVER [current http://example.com:1001/UPLOAD/FileUpload.do]

:1 {invoice, signature}

:2 File on form with name: UploadedFile

When I copy and paste : http://example.com:1001/UPLOAD/FileUpload.do on browser then it response a form like that

<head></head>
<body>
    <form enctype="multipart/form-data" action="FileUpload.do" method="post">
        <input type="file" name="UploadedFile"></input>
        <input type="hidden" value="invoice" name="type"></input>
        <input type="submit" value="Upload" name="event_Upload"></input>
    </form>
</body>

This is my code android:

HttpClient httpClient = new DefaultHttpClient();           
HttpPost httpPost = new HttpPost("http://example.com:1001/UPLOAD/FileUpload.do");
File file = new File(pathFile);
FileBody fileBody = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("event", new StringBody("Upload"));
reqEntity.addPart("type", new StringBody("invoice"));
reqEntity.addPart("UploadedFile", fileBody);

httpPost.setEntity(reqEntity);              
httpClient.execute(httpPost);

But I don't know , why it doesn't upload to server . What's wrong???

Answer

Phlip picture Phlip · Nov 2, 2013

The <form> says event_Upload; you have just event. Does that help?

See also Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar