Friday, May 27, 2011

HTTP Post Request From Android Application

HTTP Post Request From Android Application

While writing Applications in Pure Native Android Java, following is the sample piece of code which will help to make the HTTP Post request.

       try {
           HttpClient client = new DefaultHttpClient();            
           String postURL = <SERVER_URL>;
           HttpPost post = new HttpPost(postURL);            
            List<NameValuePair> params = new ArrayList<NameValuePair>();                
            params.add(new BasicNameValuePair("Key1", "Value1"));
            params.add(new BasicNameValuePair("Key2", "Value2"));                           UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
               post.setEntity(ent);      
               HttpResponse responsePOST = client.execute(post);
               HttpEntity resEntity = responsePOST.getEntity();
               if (resEntity != null) {  
                   Log.i("RESPONSE",EntityUtils.toString(resEntity));
               }
       } catch (UnsupportedEncodingException uee){
        uee.printStackTrace();
       } catch (ClientProtocolException cpe){
        cpe.printStackTrace();
       } catch (IOException ioe){
        ioe.printStackTrace();
       }


Explanation:

Creating the Objects for HttpClient and HttpPost are required so as to execute the post request.
String postURL is the Server URL, where the HttpPost request is to be made, which is in turn an argument while creating HttpPost Object.
All the request parameters in the form of the Key Value Pair are to be set in the Arraylist,

List<NameValuePair> params = new ArrayList<NameValuePair>();  

'setEntity()' methods throws 'UnsupportedEncodingException'.

'execute()' method actually makes the call to the server as follows

HttpResponse responsePOST = client.execute(post);

'client.execute()' throws 'ClientProtocolException'

Make sure to catch all the exceptions and process accordingly as per the requirements.

Finally at last make sure to add internet permission in Android Manifest file of the Application as follows:

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


Thats All For Now!

Happy Coding :-))

~Mayuri




Best Car pool Android Application Best Car Pool Application Source Code Download Here



Best Social Network Cordova Ionic Application Best Social Networking Source Code Cordova Ioinc Application Download Here


Best Android Application, Android Restaurant System Best Android Restaurant Application Source code download here


Best Android Application Android Chat Source code download Android Chat Source code download


Best Android Quiz Source Code Download Best Android Quiz Source Code Download here

More and Lots of useful Android source codes here Best Android Source codes download here



3 comments:

Bhanu Kaushik said...

great help thanx....i am a newbiee to android development rather i should say smartphone development ..this thread really helped ..thanx

Mayuri Ruparel said...

Welcome Bhanu.

Unknown said...

Gud tutorial...keep it up..!!

Animated Container in Flutter

Please check this flutter video tutorial for detailed example and implementation of Animated Container in Flutter.