0

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!

EDIT: I've looked through many websites and I understand that I should use a HttpPost. I have a few doubts though: 1. What is the url that you use in HttpPost- Is it the url which contains the form, or the url which the form redirects to. 2. In a multipartentity, what is the first parameter in addPart? Is it the ID given to the field or the name? 3. How does the HttpPost know which form it should go to?

1 Answers1

0

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!

gunar
  • 14,660
  • 7
  • 56
  • 87
  • 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 Jun 14 '13 at 17:23
  • That's because you get an Exception before httpClient.execute(..) ... you need to track it and solve it. The code I wrote there is generalistic, you need to match that to your specific business case. But I believe that is the starting point for you. – gunar Jun 16 '13 at 20:36
  • Okay, I found a website in which I want to fill a text box in a form and hit the subscribe button. The website it **http://en.blog.wordpress.com/2009/01/09/subscribe-to-comments/**. I filled my email address in the text box with field ID "subscribe-field-2" and then executed the request. I get a HTTP/1.1 200 OK but I do not receive any email from them (which is what it is supposed to do). Can you tell me what's wrong? – uttara_shekar Jun 17 '13 at 17:51