1

This is my code for making a HTTP post to an url

public static String post(String url, List<BasicNameValuePair> postvalues) {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        if ( (postvalues==null) ){
            postvalues= new ArrayList<BasicNameValuePair>();
        } 
        httppost.setEntity(new UrlEncodedFormEntity(postvalues, "UTF-8"));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        return requestToString(response);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

I'd like to add also a file to the post (not a byte[] with the final, just post the file into the PHP $_FILES field.

How can I do it?

Thanks

Addev
  • 31,819
  • 51
  • 183
  • 302
  • Exact duplicate: http://stackoverflow.com/questions/1314249/upload-and-post-file-to-php-page – Jivings Feb 11 '12 at 15:51
  • Thanks for your answer.In that question Albe is asking for uploading an image. I know how to upload an image and how to make a text but dont know how to combine both – Addev Feb 11 '12 at 16:07

1 Answers1

3

Sounds like you want to send a multi part request. This assumes that you can handle such a request on the server side:

HttpParams params = new BasicHttpParams();
for(BasicNameValuePair postValue: postValues) {
    params.setParameter(postValue.getName(), postValue.getValue());
}

HttpPost post = new HttpPost();
post.setParams(params);

MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(new File(myFile)));
post.setEntity(entity);

Alternatively to sending the name-value pairs as parameters, you can create a UrlEncodedFormEntity entity just like you are now, and add it to the multipart entity, as a separate part.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks for your answer. It returns a null pointer exception when after that code I execute httpclient.execute(post) can you help me? The stacktrace says: java.lang.NullPointerException at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) at com.com.com.MytestActivity$1.onClick(MytestActivity.java:71) ... – Addev Feb 11 '12 at 17:12
  • 1
    Can't really tell without seeing your code. Is the URL being set in the `HttpPost` constructor? – Perception Feb 11 '12 at 17:14
  • Okey it was that stupid mistake. the file is now received correctly in the PHP but the other post field don't (at least as I usually read it: $name=$_POST['name']). Here is the java code: BasicHttpParams params = new BasicHttpParams(); params.setParameter("name", "1"); HttpPost post = new HttpPost(url); post.setParams(params); MultipartEntity entity = new MultipartEntity(); entity.addPart("uploadedfile", new FileBody(new File(NAME_OF_FILE))); post.setEntity(entity);httpclient.execute(post); – Addev Feb 11 '12 at 17:24
  • 1
    Try out the `URLEncodedFormEntity` for the NameValuePairs then. – Perception Feb 11 '12 at 17:32
  • Okey calling a addPath("post_field","post_value") at the MultipartEntity finally worked. Thanks al lot for your info – Addev Feb 11 '12 at 18:09