views:

1170

answers:

3

I need to upload an image to a remote PHP server which expects the following parameters in HTTPPOST:

*$_POST['title']*
*$_POST['caption']*
*$_FILES['fileatt']*

Most of the internet searches suggested either :

Download the following classes and trying MultiPartEntity to send the request:

apache-mime4j-0.5.jar
httpclient-4.0-beta2.jar
httpcore-4.0-beta3.jar
httpmime-4.0-beta2.jar

OR

Use URLconnection and handle multipart data myself.

Btw, I am keen on using HttpClient class rather than java.net(or is it android.net) classes. Eventually, I downloaded the Multipart classes from the Android source code and used them in my project instead.

Though this can be done by any of the above mentioned methods, I'd like to make sure if these are the only ways to achieve the said objective. I skimmed through the documentation and found a FileEntity class but I could not get it to work.

What is the correct way to get this done in an Android application?

Thanks.

+1  A: 

I do exactly that to interact with an image hosting server (implemented also in php) that expects parameters as if they were posted from an html page.

I build a List<NameValuePair> of my keys and values something like this:

    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair(key1, value1));
    params.add(new BasicNameValuePair(key2, value2));

and then I pass it to my http helper class that sets the HttpEntity property of my HttpPost request. Here's the method straight out of my helper class:

public static HttpResponse Post(String url, List<NameValuePair> params, Context context)
{
    HttpResponse response = null;
    try
    {
        HttpPost request = new HttpPost();
        request.setURI(new URI(url));
        if(params != null)
            request.setEntity(new UrlEncodedFormEntity(params));
        HttpClient client = ((ApplicationEx)context.getApplicationContext()).getHttpClient();
        response = client.execute(request);
    }
    catch(Exception ex)
    {
        // log, etc
    }
    return response;
}
Rich
What type of entity object do you exactly create to encapsulate a file object?
Samuh
Well...if I owned both sides of the contract, I wouldn't use the $_FILES array...I'd send a regular key/value pair as a post param where the value is a byte stream. If I own only the client and the server strictly requires the $_FILES stuff, I would make a test html page with a simple form that does just that (file upload), trace it with fiddler, and build up the post payload manually to reflect the headers and the payload. The browser creates a "boundary" string, tells the server what the boundary is via a header, and then encloses the byte stream in this boundary. You can do the same.
Rich
That is exactly what I meant when I said: Use URLconnection and handle multipart data myself.What I really want is a class like MultipartEntity that handle all that for me. Unfortunately, this class was not imported Android. The documentation, however, enlists a FileEntity class and I was wondering how to use it.
Samuh
+1  A: 

The Android source seems to come with an internal multipart helper library. See this post. At a quick glance, it's better documented than plenty of public APIs (cough cough, SoundPool, cough cough), so it should be a pretty good place to start, or possibly fine to just use a drop-in solution.

Yoni Samlan
Yeah, I saw that thread too and managed to download the "multipart" package and use it by just changing package names.Thanks for your reply.
Samuh
You could also check out the official Apache HTTPMime project: http://hc.apache.org/httpcomponents-client/httpmime/apidocs/index.html It's based on Mime4j and I haven't used it personally, but it seems to aim to do multipart on HTTPClient 4; it'd probably work with the Android HTTPClient but I can't say for sure.
Yoni Samlan
+1  A: 

Maybe this post on the official Android group helps. The guy is using mime4j.

Another helpful resource could be this example in the Pro Anroid book.

znq