Thursday, August 23, 2012

Android Webservice Example

SOAP File form w3schools
---------------

POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/CelsiusToFahrenheit"
<?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>
<CelsiusToFahrenheit xmlns="http://tempuri.org/">
<Celsius>string</Celsius>
</CelsiusToFahrenheit>
</soap:Body>
</soap:Envelope>

-----------------------------------------------------------------------------------------------------------------------------
Android

Java Side
--------------

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

private static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

public String invoke(String val){

String reply="";
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);

// Use this to add parameters
request.addProperty("Celsius", val);

// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

//call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);

// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;

// Get the first property
reply = result!=null?result.getProperty(0).toString():"0";



} catch (Exception e) {

e.printStackTrace();
}

}

-------------------------------------------------------------------------------------------------------------------------------------

Manifest Permission
--------------------------------

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

No comments:

Post a Comment