Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an Android device. I want to fill a form in my app, with edittexts etc (one of these fields would take the path of an image on the SDCard). I want these form contents to be the data for an HTML form in an external website where this file (from the SD Card) needs to be uploaded. The HTML form has an upload button. I do not want to show this HTML webpage to my android app users. Is there any way to do this? Please let me know! Thanks!

share|improve this question

1 Answer

Well, you need to make a MultiPart Http Post. You could use this sample:

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("target_link");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("data1", new StringBody("Data1"));
reqEntity.addPart("data2", new StringBody("Data2"));
reqEntity.addPart("data3",new StringBody("Data3"));
try{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 80, bos);
    byte[] data = bos.toByteArray();
    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
    reqEntity.addPart("picture", bab);
}
catch(Exception e){
    //Log.v("Exception in Image", ""+e);
    reqEntity.addPart("picture", new StringBody(""));
}
postRequest.setEntity(reqEntity);       
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
    s = s.append(sResponse);
}

Personally, I prefer to use Spring for Android as that is easier to configure. Here's a link with a multi-part Http Post.

Good luck!

share|improve this answer
Thanks for the answer! Well, I did try that, but the command HttpResponse response = httpClient.execute(postRequest); does not execute. I put break points before and after this command and for some reason, the httpClient does not execute the postRequest, I don't understand what's wrong. – uttara_shekar 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.