Monday 5 September 2011

HTTP Request

Code snippet to post data to server:

The following code sends data to server using post request method.

            HttpParams httpParams = null;
            HttpClient httpclient =  null;
            HttpPost httppost = null;
            List<NameValuePair> nameValuePairs = null;

            HttpResponse response = null;

            httpParams = new BasicHttpParams();
            httpclient =  new DefaultHttpClient(httpParams);
            httppost = new HttpPost("Give Server URL");
            nameValuePairs = new ArrayList<NameValuePair>();
            // post data parameters
            nameValuePairs.add(new BasicNameValuePair("deviceId", deviceId));
            try
            {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);
                int statusCode = response.getStatusLine().getStatusCode();
                Log.i("Status Code ->  ",statusCode+"");
                if(statusCode != 200)
                {
                    showDialog("Connection Error.Please Try again laterlll !!!");
                }
             } catch (Exception e)
             {
                 Log.e("Exception - Uploading data to server",e.getMessage());
             }

No comments:

Post a Comment