1

I'm trying to upload a photo from android device to a php website. For this I'm using MultipartEntity encoding.

For this I added to my project as an external jar file httpmime-4.1-beta1.jar.

What I want to upload to the php website is a image stored on the SDcard. To this end I'm doing the following in my code:

HttpPost httppost = new HttpPost("....the link to the webiste...");

        MultipartEntity reqEntity = new MultipartEntity();
        StringBody sb=new StringBody("....");


        File file=new File("/sdcard/image.jpg");
        FileBody bin = new FileBody(file);
        reqEntity.addPart("android",bin);
        reqEntity.addPart("id", sb);

        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();


       if (resEntity != null) {
            String page = EntityUtils.toString(resEntity);
            System.out.println("PAGE :" + page);
        }

But the problem with this is that the response from php server is always a broken link.

What I wanna try next is to use

 MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

But unfortunately the jar I imported doesn't has the class for HttpMultipartMode.BROWSER_COMPATIBLE.So I would really appreciate if you could point me in the right direction-what else should I import for this to work....or how should I upload the image to the server. I must say that the server is build to upload photo this way:

method="post" enctype="multipart/form-data"
                      name="form" target="_self" id="form">
                    <input type="hidden" name="p" value="a" />

Thanks!

1

1 Answer 1

2

I would recommend not to use a beta library. You should use apache-mime4j-0.6 .

You will also need httpmime-4.0.1.

These need to be your imports:

      import org.apache.http.HttpResponse;
      import org.apache.http.client.ClientProtocolException;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.entity.UrlEncodedFormEntity;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.entity.mime.HttpMultipartMode;
      import org.apache.http.entity.mime.MultipartEntity;
      import org.apache.http.entity.mime.content.FileBody;
      import org.apache.http.entity.mime.content.StringBody;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;
Sign up to request clarification or add additional context in comments.

6 Comments

Now it tells me:The type org.apache.james.mime4j.message.SingleBody cannot be resolved. It is indirectly referenced from required .class files at this lines: MultipartEntity reqEntity = new MultipartEntity();reqEntity.addPart("android",bin); reqEntity.addPart("id", sb);
Am importat deja librariile alea, am adaugat zeci de jar uri si tot imi da eroare la linia respectiva!
import that class too then import org.apache.james.mime4j.message.SingleBody;
Crezi ca o recunoaste?Nu exista clasa respectiva in librarii!:)
|

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.