You are on page 1of 5

Thc h nh

1. To project tn soap_demo1. (Nh to trn my o 1.6 cho n gin)


2. To th mc mi tn libs trong th mc gc ca project.

3. Truy cp a ch (hoc ln google search cho l)

https://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-
android/ksoap2-android-assembly/2.6.0?r=803

v download file "ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar" . B file va download


vo th mc libs va to.

4. Click phi project chn Build Path-> Configue Build Path.


5. Trong hp thoi Properties chn tab Libraries chn nt Add JARs chn n file ksoap2 va mi
chp vo ri OK -> OK.

6. thy hnh ca file trong th mc lib c thm ci hnh icon nh gc.

7. M file layout thit k giao din c 2 TextView lm nhn, 1 EditText nhp F, 1 EditText nhp C, 1
button i C sang F , 1 button i F sang C v 1 button Xa:
8. Truy cp a ch http://www.w3schools.com/webservices/tempconvert.asmx nhn vo hm u tin
(CelsiusToFahrenheit) ko xung bn di v phn tch c php.

Cho CelsiusToFahrenheit:

1. SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
2. NAMESPACE = "http://tempuri.org/";
3. METHOD_NAME = "CelsiusToFahrenheit";

4. URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Cho FahrenheitToCelsius:

1. SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
2. NAMESPACE = "http://tempuri.org/";
3. METHOD_NAME = " FahrenheitToCelsius ";

4. URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
9. M file manifest v khai bo quyn internet

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

10. Trong file java chnh, khai bo cc bin, nh x chng.

EditText etdoC,etdoF;
Button btFtoC, btCtoF,btclear;

11. Khai bo cc bin chui chun b.

private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";


private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL =
"http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

12. Bt s kin onClick cho nt btFtoC. Trong hm onClick ca n bt u vit nh bn di gm khi to


SoapObject, gn d liu vo, khai bo envelope, v gn outputSoapObject cho envelope:

//khoi tao soap request va param


SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME1);

//dua du lieu vao cho request


request.addProperty("Fahrenheit",etdoF.getText().toString());

//khai bao Envelope va dua vao version cua SOAP


SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;

13. Tip theo khai bo Transport v a vo URL. Sau gi hm call gi service v truyn vo action v
envelope.

//khoi tao httptransport


HttpTransportSE httptransport=new HttpTransportSE(URL);

//goi webservice
try {
httptransport.call( SOAP_ACTION1, envelope);
} catch (Exception e) {
Log.d("loi"," "+ e.toString());
}

14. Cui cng ly d liu v v gn ln EditText.

//lay du lieu
SoapObject ketqua=(SoapObject)envelope.bodyIn;
if(ketqua!=null)
{
etdoC.setText(ketqua.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "khong co ket qua",
Toast.LENGTH_SHORT).show();
}

15. Chy chng trnh (nh phi c internet). Nhp vo 1 con s F, nhn nt i F sang C thy kt qu.

16. Vit tip cho nt i C sang F.


17. Nng cp version ln 4.0 chy th thy li. Sa li chng trnh c th chy tt trn cc version cao.

You might also like