Reputation: 4594
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!
Upvotes: 1
Views: 4063
Reputation: 72311
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;
Upvotes: 2