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.
or
Web service is a web Application, which can publish its function or message to the rest of the world.
SOAP MESSAGE FORMAT
<?xml version="1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header> ... ... </SOAP-ENV:Header>
<SOAP-ENV:Body> ... ... <SOAP-ENV:Fault> ... ... </SOAP-ENV:Fault> </SOAP-ENV:Body>
</SOAP_ENV:Envelope>
Description:
- 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.setRequestMethod("GET"); con.setDoInput(true); con.addRequestProperty("Referer", "http://nsolution.in"); // Start the query con.connect(); is = con.getInputStream(); }catch (IOException e) { //handle the exception ! e.printStackTrace(); } return is; }
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityname=(EditText)findViewById(R.id.editText1);
countryname=(EditText) findViewById(R.id.editText2);
tv=(TextView)findViewById(R.id.textView1);
}
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");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
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=\"
http://www.w3.org/2001/XMLSchema\">"
+"<SOAP-ENV:Body>"
+ "<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:details>"
+"<m:price>"+ price.getText() +"</m:price>"
+"</m:saveInventory>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV: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>"
+"<saveInventory xmlns=\"http://service.com\">"
+"<barId>barcodeid.getText()</barId>"
+"< name>name.getText()</name>"
+ "< model>model.getText()</model>"
+"< details>details.getText()</details>"
+"< price>price.getText()</ price>"
+ "</ saveInventory>"
+"</soap:Body>"
+" </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:Body>"
+ " </soap:Envelope>";
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("body1==" + body1);
wr.write(body1);
GpsLogs.InfoLog("callWebService - before flush " + body1);
wr.flush(); // Get
GpsLogs.InfoLog("callWebService - before bufferedReader ");
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
mResult = mResult + line;
}
GpsLogs.InfoLog("mResult==" + mResult);
} catch (Exception e) {
e.printStackTrace();
}
return mResult;
}
private boolean domParseForResult(String parse) throws Exception {
try {
boolean flag = false;
MenuItemBean resultBean;
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new InputSource(
new StringReader(parse)));
// normalize the document
doc.getDocumentElement().normalize();
// get the root node
NodeList nodeList =
doc.getElementsByTagName("GetWeatherResponse");
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);
if
(temp.getNodeName().equalsIgnoreCase("GetWeatherResult")) {
String result = temp.getTextContent();
tv.setText(result);
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 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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityname=(EditText)findViewById(R.id.editText1);
countryname=(EditText) findViewById(R.id.editText2);
tv=(TextView)findViewById(R.id.textView1);
}
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");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
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=\"
http://www.w3.org/2001/XMLSchema\">"
+"<SOAP-ENV:Body>"
+ "<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:details>"
+"<m:price>"+ price.getText() +"</m:price>"
+"</m:saveInventory>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV: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>"
+"<saveInventory xmlns=\"http://service.com\">"
+"<barId>barcodeid.getText()</barId>"
+"< name>name.getText()</name>"
+ "< model>model.getText()</model>"
+"< details>details.getText()</details>"
+"< price>price.getText()</ price>"
+ "</ saveInventory>"
+"</soap:Body>"
+" </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:Body>"
+ " </soap:Envelope>";
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("body1==" + body1);
wr.write(body1);
GpsLogs.InfoLog("callWebService - before flush " + body1);
wr.flush(); // Get
GpsLogs.InfoLog("callWebService - before bufferedReader ");
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
mResult = mResult + line;
}
GpsLogs.InfoLog("mResult==" + mResult);
} catch (Exception e) {
e.printStackTrace();
}
return mResult;
}
private boolean domParseForResult(String parse) throws Exception {
try {
boolean flag = false;
MenuItemBean resultBean;
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new InputSource(
new StringReader(parse)));
// normalize the document
doc.getDocumentElement().normalize();
// get the root node
NodeList nodeList =
doc.getElementsByTagName("GetWeatherResponse");
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);
if
(temp.getNodeName().equalsIgnoreCase("GetWeatherResult")) {
String result = temp.getTextContent();
tv.setText(result);
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);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="29dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:onClick="callWebservice"
android:text="Get Update" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="34dp"
android:layout_marginTop="40dp"
android:text=""
/>
</RelativeLayout>
Declare this permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
0 coment�rios:
Post a Comment