`
jiagou
  • 浏览: 2530337 次
文章分类
社区版块
存档分类
最新评论

android解决UI阻塞问题——创建AsyncTask 子线程

 
阅读更多
Java代码
  1. viewplaincopytoclipboardprint?
  2. viewplaincopytoclipboardprint?
  3. packagenet.blogjava.mobile.wsclient;
  4. importorg.ksoap2.SoapEnvelope;
  5. importorg.ksoap2.serialization.SoapObject;
  6. importorg.ksoap2.serialization.SoapSerializationEnvelope;
  7. importorg.ksoap2.transport.HttpTransportSE;
  8. importandroid.app.Activity;
  9. importandroid.os.AsyncTask;
  10. importandroid.os.Bundle;
  11. importandroid.view.View;
  12. importandroid.view.View.OnClickListener;
  13. importandroid.widget.Button;
  14. importandroid.widget.EditText;
  15. importandroid.widget.TextView;
  16. publicclassMainextendsActivityimplementsOnClickListener
  17. {
  18. privateEditTextetProductName;
  19. privateTextViewtvResult;
  20. classWSAsyncTaskextendsAsyncTask
  21. {
  22. Stringresult="";
  23. @Override
  24. protectedObjectdoInBackground(Object...params)
  25. {
  26. try
  27. {
  28. StringserviceUrl="http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl";
  29. StringmethodName="getProduct";
  30. SoapObjectrequest=newSoapObject("http://service",
  31. methodName);
  32. request.addProperty("productName",etProductName.getText().toString());
  33. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  34. SoapEnvelope.VER11);
  35. envelope.bodyOut=request;
  36. HttpTransportSEht=newHttpTransportSE(serviceUrl);
  37. ht.call(null,envelope);
  38. if(envelope.getResponse()!=null)
  39. {
  40. SoapObjectsoapObject=(SoapObject)envelope.getResponse();
  41. result="产品名称:"+soapObject.getProperty("name")+"\n";
  42. result+="产品数量:"+soapObject.getProperty("productNumber")
  43. +"\n";
  44. result+="产品价格:"+soapObject.getProperty("price");
  45. }
  46. else
  47. {
  48. result="无此产品.";
  49. }
  50. }
  51. catch(Exceptione)
  52. {
  53. result="调用WebService错误.";
  54. }
  55. //必须使用post方法更新UI组件
  56. tvResult.post(newRunnable()
  57. {
  58. @Override
  59. publicvoidrun()
  60. {
  61. tvResult.setText(result);
  62. }
  63. });
  64. returnnull;
  65. }
  66. }
  67. @Override
  68. publicvoidonClick(Viewview)
  69. {
  70. //异步执行调用WebService的任务
  71. newWSAsyncTask().execute();
  72. }
  73. @Override
  74. publicvoidonCreate(BundlesavedInstanceState)
  75. {
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.main);
  78. ButtonbtnSearch=(Button)findViewById(R.id.btnSearch);
  79. btnSearch.setOnClickListener(this);
  80. etProductName=(EditText)findViewById(R.id.etProductName);
  81. tvResult=(TextView)findViewById(R.id.tvResult);
  82. }
  83. }

1. 一般需要编写一个AsyncTask的子类来完成后台执行任务的工作。
2. AsyncTask的核心方法是doInBackground,当调用AsyncTask类的execute方法时,doInBackground方法会异步执行。因此,可以将执行任务的代码写在doInBackground方法中。
3.由于本例中的TextView组件是在主线程(UI线程)中创建的,因此,在其他的线程(doInBackground方法所在的线程)中不能直接更新TextVew组件。为了更新TextView组件,需要使用TextView类的post方法。该方法的参数是一个Runnable对象,需要将更新TextView组件的代码写在Runnable接口的run方法中。
4.虽然不能在其他线程中更新UI组件,但可以从其他线程直接读取UI组件的值。例如,在doInBackground方法中直接读取了EditText组件的值。
5.调用AsyncTask类的execute方法后会立即返回。execute方法的参数就是doInBackground方法的参数。doInBackground方法的返回值可以通过AsyncTask.execute(...).get()方法获得。
读者可以将本例中的IP改成其他的值,看看单击按钮后,是否还可在文本框中输入其他的内容。如果这个IP是正确的,并且WebService可访问,那么会在TextView组件中输出相应的返回值。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics