0

I'm trying to upload a XML file from an android device to a URL where it will be parsed. As an example I was given a curl command, which works:

curl -F [email protected] "http://the-url-to-the-server"

But when I try to send something from my Android device I keep getting 'Invalid xml' as response. So based on this answer I created a php file for myself to try sending the file to, to see how it gets send:

<?php
print_r($_FILES);
print_r($_REQUEST);
?>

I've been trying all kinds of example code, but they all are rather similar, so I think they must be the right way to do it.

Basically it comes down to this: Either I send the file without declaring the MIME-type (see foo below), and then it get send as "application/octet-stream", or I add a MIME-type "text/xml" or "application/xml" (see bar below) and then it ends up in the $_REQUEST of php, which I assume means it was not send as a file.

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("foo", new FileBody(file));
reqEntity.addPart("bar", new FileBody(file, "text/xml"));

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

gives

Array
(
    [foo] => Array
    (
        [name] => templ2.xml
        [type] => application/octet-stream
        [tmp_name] => /tmp/phppWbgl8
        [error] => 0
        [size] => 203
    )
)
Array
(
    [bar] => <foo>
            <bar>
            </bar>
            </foo>
)

Some related questions, which I have tried the code from:

2 Answers 2

0

Instead of

reqEntity.addPart("bar", new FileBody(file, "text/xml"));

Try doing this

reqEntity.addPart("bar");
httpPost.addHeader("Content-Type", "application/xml");
Sign up to request clarification or add additional context in comments.

1 Comment

Then you would change the content type of the entire request, which I believe should be some form of multipart. Anyway I tried it and didn't help.
0

I figured out my problem. When I looked at the curl command, I realised [email protected] means I had to use xml as name for the parameter. So

reqEntity.addPart("xml", new FileBody(file));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.