Server Client Communication in android Part-II

Server Client Communication

Now lets move on to the coding part of the server client communication in android. As a wrote in my previous blog about the  brief introduction of the server client concept used in android .Now hows its done lets view it

Firstly u need a url where you server file resides for example localhost/myapp/index.php  or 192.165.17.1/myapp/index.php

String url="where server file reside";

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            String done=null;
            try {
                 //created a arraylist of NameValuePair it is class encapsulating a name/value pair.
                // For more refer                                  // https://hc.apache.org/httpclient-.x/apidocs/org/apache/commons/httpclient/NameValuePair.html

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("phoneno", "xxxxxxxxxx"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                response.getEntity().consumeContent();
                String response1;
           ResponseHandler<String> responseHandler = new BasicResponseHandler();
           response1= httpclient.execute(httppost, responseHandler);
             //the response1  is the reply send by the serve when the client send a request
                 }
              catch(Exception e)
            {
            e.printStackTrace();
            }

The above is the easiest code that is being used.To make this code working you have to run this part of code in a thread otherwise it does not give any result at all.

In case of Android two methods are being used one is the Threads as that you have found in Java and the other one is built for android that is AsynTask.

In case of php you can write something like that

mysample.php

$number=$_POST["phoneno"];
if(isempty($name)
{
echo "success";
}else
echo "failure";


Creating Database for Android to learn hows it Click on this link http://mithunmanoharan.blogspot.com/2014/12/creating-database-at-server-using.html

Comments

Popular posts from this blog

Making Bootable Pendrive with .bat file

Ek Beej Tha Gaya Bahut Hee Gaharaee Mein Boya - Nanha Paudha

R-W-W (Real, Win, Worth it ) - Boost your idea