HTTP Post Request in Android Example

HTTP post request are executed in android in a very simple way. You need just 4 steps.

1. Declare Internet permissions in the manifest by adding the following line to AndroidManifest.xml.


<uses-permission android:name="android.permission.INTERNET" />


2. Create your HttpClient and HttpPost objects to execute the POST request.


HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);


String Object like String address = "www.google.com";

3. Set your POST data

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
post.setEntity(new UrlEncodedFormEntity(pairs));


4. Execute the POST request

This returns an HttpResponse object, whose data can be extracted and parsed.

HttpResponse response = client.execute(post);




A closer look into struts-config.xml in Struts 1.x

A web application uses a deployment descriptor to initialize resources like servlets and taglibs. This deployment descriptor is formatted as an XML file and named "web.xml". Likewise, the framework uses a configuration file to initialize its own resources. These resources include ActionForms to collect input from users, ActionMappings to direct input to server-side Actions, and ActionForwards to select output pages.

Here's a simple struts-config.xml file and its different elements:

<struts-config>

<form-beans> 
  <form-bean 
       name="helloWorldForm" 
     type="com.naeemgik.common.form.HelloWorldForm"/>
</form-beans>

<action-mappings>
  <action 
       path="/helloWorld"
       input="/ByeWorld.jsp"
type="com.naeemgik.common.action.HelloWorldAction"
name="helloWorldForm"
       attribute="helloWorldForm"
       validate="true"
       scope="request"
       parameter="operation">

<forward name="success" path="/HelloWorld.jsp"/>
       <forward name="error" path="/ByeWorld.jsp"/>

  </action>
</action-mappings>

</struts-config>


In the above struts-config.xml file I have included most of the attributes of the action tag for the understanding point of view. Now let discuss each one.

The form-bean tag have HelloWorldForm which is used to hold the data. This class is extended from ActionForm.java

The acion tag attributes:

path: The action will be only and only called when the the user hit the URL of named /helloWorld

input: If vaidate is true and the form have some validation errors then the default page to display will be mentioned in this attribute.

type: Type is the Action class that will be called. It may be extended from Action.java or DispatchAction.java depending upon user scenarios. For more details may refer to Difference between Action and DispatchAction

name: The “name” attribute is actually the form-bean name.

attribute: attribute have same meaning as that of name attribute.

validate: This attribute is used whether this action needs some sort of validators or not? In case validate="false" it skips any validations.

scope: This actaully describe the action scope whether it will be request or session.

parameter: This is used only in case of DispatchAction. As dispatch Action have multiple action methods so programmer must have explicitly mention which action needs to be triggered. If you are new to Action and DispatchAction must read Difference between Action and DispatchAction

forward: This tag actually launches the corresponding JSP,page or another action depending on the forward name attribute.




Difference between Action and DispatchAction


Below are the main differences between Struts Action and Dispatch Actions

  • Grouping related actions into one class is possible using DispatchAction class. But in Action class a group related action into one class is not possible..Several changes need to be done, when one plans to group the related action using Action class.
  • Action class is used to perform single functionality. To perform multiple functionalities, we need to write an additional Action class.


  • The complexity rises if Action class is used for multiple functionalities, whereas by using DispatchAction, the code complexity will noticeably be reduced.
  • An Action class acts as a controller and is an adapter class between the web tier and the business tier.
  • Dispatch class is used to combine the related operations, thus share the common resources, helper classes for instance.
  • An Action is invoked by the ActionServlet to perform an operation that is depending on the URL request.
  • A DispatchAction selects a method depending on the value of the request parameter that is configured in the xml file.