Learn Android for Free,issues for developers, mobengineers website provide complete solutions for developers issues posts.It also provides online training for android development

Sunday, July 1, 2012

Web Services

                    Web services standards and technologies allow us to describe and deploy applications or services on a network in a consistent way so that they can be discovered and invoked in a secure and reliable manner. or   Web services is a software system. i.e. it consists of collection of programs.
                    Web service is a web Application, which can publish its function or message to the rest of the world. 



<?xml version="1.0"?>

  • Envelope: ( Mandatory )
    Defines the start and the end of the message.
  • Header: ( Optional )
    Contains any optional attributes of the message used in processing the message, either at an intermediary point or at the ultimate end point.
  • Body: ( Mandatory )
    Contains the XML data comprising the message being sent.
  • Fault: ( Optional )
    An optional Fault element that provides information about errors that occurred while processing the message

    Getting stream using url

    private InputStream downloadUrl(String url) {
      HttpURLConnection con = null;
      URL url;
      InputStream is=null;
      try {
       url = new URL(url);
       con = (HttpURLConnection) url.openConnection();
       con.setReadTimeout(10000 /* milliseconds */);
       con.setConnectTimeout(15000 /* milliseconds */);
       con.addRequestProperty("Referer", "http://nsolution.in");
       // Start the query
       is = con.getInputStream();
      }catch (IOException e) {
                            //handle the exception !
      return is;
     Using Http
 public static InputStream getInputStreamFromUrl(String url) {
  InputStream content = null;
  try {
   HttpGet httpGet = new HttpGet(url);
   HttpClient httpclient = new DefaultHttpClient();
   // Execute HTTP Get Request
   HttpResponse response = httpclient.execute(httpGet);
   content = response.getEntity().getContent();
                } catch (Exception e) {
   //handle the exception !
  return content;


WSDL stands for  "Web Services Description Language"

  • WSDL stands for Web Services Description Language
  • WSDL is an XML based protocol for information exchange in decentralized and distributed environments.
  • WSDL is the standard format for describing a web service.
  • WSDL definition describes how to access a web service and what operations it will perform.
  • WSDL is a language for describing how to interface with XML-based services.
  • WSDL is an integral part of UDDI, an XML-based worldwide business registry.
  • WSDL is the language that UDDI uses.
  • WSDL was developed jointly by Microsoft and IBM.

UDDI stands for "Universal Description Discovery Integration

UDDI is an XML-based standard for describing, publishing, and finding Web services.

  • UDDI stands for Universal Description, Discovery and Integration.
  • UDDI is a specification for a distributed registry of Web services.
  • UDDI is platform independent, open framework.
  • UDDI can communicate via SOAP, CORBA, Java RMI Protocol.
  • UDDI uses WSDL to describe interfaces to web services.
  • UDDI is seen with SOAP and WSDL as one of the three foundation standards of web services.
  • UDDI is an open industry initiative enabling businesses to discover each other and define how they interact over the Internet.
UDDI has two parts:
  • A registry of all a web service's metadata including a pointer to the WSDL description of a service
  • A set of WSDL port type definitions for manipulating and searching that registry

here is the simple example for calling webservices.

Hi , i will explain dom parsing with a simple example. but if we do this with sax or pull parsing is better because , dom parsing is for large documents. just for understanding and simplicity of dom code, i am doing this by using dom parsing.

package com.example.webservice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 private EditText cityname=null;
    private EditText countryname=null;
    String tempVal = "";
    String res="";
    private String mResult = "";
   private TextView tv;
    public void onCreate(Bundle savedInstanceState) {
        countryname=(EditText) findViewById(R.id.editText2);

    public String callWebservice(View v) {
try {
            URL mUrl = new URL("http://www.webservicex.net/globalweather.asmx");

            //HotelMenuDatabase database = new HotelMenuDatabase(context);
            GpsLogs.InfoLog("callWebService - before url connection ");
            URLConnection conn = mUrl.openConnection();
            // URLConnection conn = mUrl.openConnection();

            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.addRequestProperty("SOAPAction", "http://www.webserviceX.NET/GetWeather");


            OutputStreamWriter wr = new OutputStreamWriter(
            String body1 = "", body2 = "";
            //Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
            GpsLogs.InfoLog("callWebService - before body ");
            try {
                 body1="<?xml version=\"1.0\" encoding=\"utf-8\"?>"   
                            +"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"
http://schemas.xmlsoap.org/soap/envelope/\"  xmlns:SOAP-ENC=\"
http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"
                            + "<m:saveInventory xmlns:m=\"http://service.com
                            +"<m:barId>"+ barcodeid.getText() +" </m:barId>"
                            +"<m:name>"+ name.getText() +"</m:name>"
                            +"<m:model>"+ model.getText() +"</m:model>"
                            +"<m:details>"+ details.getText()
                            +"<m:price>"+ price.getText() +"</m:price>"
                 /*body1=      "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                            + "<soap:Envelope xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"
http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"
                            + "<soap:Body>"
                            +"<saveInventory xmlns=\"http://service.com\">"
                       +"< name>name.getText()</name>"
                      + "< model>model.getText()</model>"
                       +"< details>details.getText()</details>"
                       +"< price>price.getText()</ price>"
                    + "</ saveInventory>"
                +" </soap:Envelope>";



                  body1=    "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
               +  " <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                   + "<soap:Body>"
                     + "<GetWeather xmlns=\"http://www.webserviceX.NET\">"
                       + "<CityName>hyderabad</CityName>"
                      +  "<CountryName>india</CountryName>"
                     + "</GetWeather>"
                + " </soap:Envelope>";
            catch (Exception e) {
            System.out.println("body1==" + body1);
            GpsLogs.InfoLog("callWebService - before flush " + body1);
            wr.flush(); // Get

            GpsLogs.InfoLog("callWebService - before bufferedReader ");
            BufferedReader rd = new BufferedReader(new InputStreamReader(
            String line;
            while ((line = rd.readLine()) != null) {
                mResult = mResult + line;

            GpsLogs.InfoLog("mResult==" + mResult);


        } catch (Exception e) {
        return mResult;

    private boolean domParseForResult(String parse) throws Exception {
        try {
            boolean flag = false;
            MenuItemBean resultBean;
            DocumentBuilderFactory dbf =
            DocumentBuilder db = dbf.newDocumentBuilder();
            org.w3c.dom.Document doc = db.parse(new InputSource(
                    new StringReader(parse)));
            // normalize the document
            // get the root node
            NodeList nodeList =
            int length = nodeList.getLength();
            // GpsLogs.InfoLog("total records is" + length);
            for (int j = 0; j < length; j++) {
                resultBean = new MenuItemBean();
                Node node = nodeList.item(j);
                // the node has three child nodes
                for (int i = 0; i < node.getChildNodes().getLength(); i++) {

                    Node temp = node.getChildNodes().item(i);

(temp.getNodeName().equalsIgnoreCase("GetWeatherResult")) {
                        String result = temp.getTextContent();
                        GpsLogs.InfoLog("result==" + result);
                        /*if (s.equalsIgnoreCase(result)) {

                            System.out.println("response" + result);
                            return true;
        } catch (Exception e) {
        return false;


package com.example.webservice;

public class MenuItemBean {

public String resultBean;


package com.example.webservice;

import android.util.Log;

public class GpsLogs {

private static final String tag="weather";
public static void ExceptionLog(String ex)
Log.e(tag, ex);
public static void InfoLog(String info)
Log.i(tag, info);

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" >

        android:ems="10" >

        <requestFocus />

        android:ems="10" />

        android:text="Get Update" />



Declare this permission in manifest file

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

0 coment�rios:

Post a Comment

20% off

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

NDTV Gadgets360

Blog Archive

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts