Document 60 Soap

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Lp trnh Android

Bi
59

Tng tc dch v
SOAP

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Ni dung bi hc

Giao thc SOAP (Simple Object


Access Protocol)
Cch dng th vin KSOAP API
Mt s trng hp thng gp
Ly gi tr n (primitive data)
Ly gi tr i tng (1 i tng)
Ly gi tr l danh sch i tng
(mng)

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

ksoap2-android-assembly-2.6.0-jar-withdependencies.jar

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Demo ly gi tr n dng
WCF Client Test
public class Configuration {
public static String
SERVER_URL="http://www.w3schools.com/xml/tempconvert.asmx?WSDL";
public static String NAME_SPACE="http://www.w3schools.com/xml/";
public static String METHOD_C_TO_F="CelsiusToFahrenheit";
public static String PARAMETER_CELSIUS="Celsius";
public static String SOAP_C_TO_F=NAME_SPACE+METHOD_C_TO_F;
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

protected String doInBackground(String... params) {


String kq="";
try
{
String c=params[0];
//To 1 yu cu gi ln server:
SoapObject request=new SoapObject
(Configuration.NAME_SPACE,Configuration.METHOD_C_TO_F);
//Nu yu cu ny c i s (parameter):
request.addProperty(Configuration.PARAMETER_CELSIUS,c);
//to Envelope
SoapSerializationEnvelope envelope=new
SoapSerializationEnvelope(SoapSerializationEnvelope.VER11
envelope.dotNet=true;
//gn request cho envelope
envelope.setOutputSoapObject(request);
//Nu c truyn d liu kiu float,double qua li th phi ng k
MarshalFloat marshalFloat=new MarshalFloat();
marshalFloat.register(envelope);
//Nu c truyn date qua li:
MarshalDate marshalDate=new MarshalDate();
marshalDate.register(envelope);
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

//to loi kt ni ln server


HttpTransportSE se=new
HttpTransportSE(Configuration.SERVER_URL);
//gi lnh thc hin hm (hm ny thc s x l trn
Server ri):
se.call(Configuration.SOAP_C_TO_F,envelope);
//v kt qu tr v theo m t l kiu chui (hm ny l ly
kt qu sau khi x l):
//d liu n (dng SoapPrimitive)
SoapPrimitive result= (SoapPrimitive) envelope.getResponse();
kq=result.toString();
}
catch (Exception ex)
{
kq=ex.toString();
}
return kq;
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Demo tr v mt i
tng

HttpTransportSE se=new HttpTransportSE(Configuration.SERVER_URL);


se.call(Configuration.SOAP_GET_ONE_PRODUCT,envelope);

SoapObject result= (SoapObject) envelope.getResponse();


if(result.hasProperty("ProductId"))
product.setProductId(result.getPropertyAsString("ProductId"));
if(result.hasProperty("ProductName"))
product.setProductName(result.getPropertyAsString("ProductName"));
if(result.hasProperty("Quantity"))
product.setQuantity(Integer.parseInt(result.getPropertyAsString("Quan
if(result.hasProperty("UnitPrice"))
product.setUnitPrice(Double.parseDouble(result.getPropertyAsString("U
if(result.hasProperty("TotalMoney"))
product.setTotalMoney(Double.parseDouble(result.getPropertyAsString("

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Demo tr v danh sch


i tng

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

Demo tr v danh sch


i tng

HttpTransportSE transportSE=new HttpTransportSE(Configuration.SERVER_URL);


transportSE.call(Configuration.SOAP_ACTION_GET_LIST_PRODUCT,envelope);

//v tr v danh sch Product ==> SoapObject


SoapObject arrProduct= (SoapObject) envelope.getResponse();
//Ta cn duyt cc phn t nm bn trong SoapObject ny (c th l arrProduct):
//arrProduct.getPropertyCount() l tr v s lng phn t trong danh sch
for (int i=0;i<arrProduct.getPropertyCount();i++)
{
//Ly phn t th i ra:
SoapObject objProduct= (SoapObject) arrProduct.getProperty(i);
//Tip theo ta tin hnh c tng d liu trong objProduct to thnh Product
Product product=new Product();
if(objProduct.hasProperty("id"))
{
product.setId(Integer.parseInt(objProduct.getPropertyAsString("id")));
}
if(objProduct.hasProperty("name"))
{
product.setName(objProduct.getPropertyAsString("name"));
}
if(objProduct.hasProperty("unitprice"))
{
product.setUnitprice(Integer.parseInt(objProduct.getPropertyAsString("unitprice"))
);
}
//a product vo danh sch product:
ds.add(product);
}

Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

Lp trnh Android

END
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com

You might also like