异步任务
01.view plaincopy to clipboardprint?
02.view plaincopy to clipboardprint?
03.package net.blogjava.mobile.wsclient;
04.
05.import org.ksoap2.SoapEnvelope;
06.import org.ksoap2.serialization.SoapObject;
07.import org.ksoap2.serialization.SoapSerializationEnvelope;
08.import org.ksoap2.transport.HttpTransportSE;
09.import android.app.Activity;
10.import android.os.AsyncTask;
11.import android.os.Bundle;
12.import android.view.View;
13.import android.view.View.OnClickListener;
14.import android.widget.Button;
15.import android.widget.EditText;
16.import android.widget.TextView;
17.
18.public class Main extends Activity implements OnClickListener
19.{
20. private EditText etProductName;
21. private TextView tvResult;
22.
23. class WSAsyncTask extends AsyncTask
24. {
25. String result = "";
26. @Override
27. protected Object doInBackground(Object... params)
28. {
29. try
30. {
31. String serviceUrl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl";
32. String methodName = "getProduct";
33. SoapObject request = new SoapObject("http://service",
34. methodName);
35. request.addProperty("productName", etProductName.getText().toString());
36. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
37. SoapEnvelope.VER11);
38. envelope.bodyOut = request;
39. HttpTransportSE ht = new HttpTransportSE(serviceUrl);
40.
41. ht.call(null, envelope);
42. if (envelope.getResponse() != null)
43. {
44. SoapObject soapObject = (SoapObject) envelope.getResponse();
45. result = "产品名称:" + soapObject.getProperty("name") + "\n";
46. result += "产品数量:" + soapObject.getProperty("productNumber")
47. + "\n";
48. result += "产品价格:" + soapObject.getProperty("price");
49.
50. }
51. else
52. {
53. result = "无此产品.";
54. }
55. }
56. catch (Exception e)
57. {
58. result = "调用WebService错误.";
59. }
60. // 必须使用post方法更新UI组件
61. tvResult.post(new Runnable()
62. {
63. @Override
64. public void run()
65. {
66. tvResult.setText(result);
67.
68. }
69. });
70. return null;
71. }
72.
73. }
74. @Override
75. public void onClick(View view)
76.{
77. // 异步执行调用WebService的任务
78. new WSAsyncTask().execute();
79. }
80. @Override
81. public void onCreate(Bundle savedInstanceState)
82. {
83. super.onCreate(savedInstanceState);
84. setContentView(R.layout.main);
85. Button btnSearch = (Button) findViewById(R.id.btnSearch);
86. btnSearch.setOnClickListener(this);
87. etProductName = (EditText) findViewById(R.id.etProductName);
88. tvResult = (TextView) findViewById(R.id.tvResult);
89.
90. }
91.}