Android平台调用Web Service:线程返回值

来源:互联网 发布:金山数据恢复和 编辑:程序博客网 时间:2024/05/17 22:16

接上文

前文中的遗留问题

对于Java多线程的理解,我以前仅仅局限于实现Runnable接口或者继承Thread类,然后重写run()方法,最后start()调用就算完事,但是一旦涉及死锁以及对共享资源的访问和随时监控线程的状态和执行顺序和线程返回值等就不行了。

 

Callable 和 Future 简介

Callable接口代表一段可以调用并返回结果的代码;Future接口表示是执行异步任务时的状态、返回值等信息。所以说Callable用于产生结果,Future用于获取结果。

 

1. Callable

Callable 是一个接口,它只包含一个call()方法。Callable是一个返回结果并且可能抛出异常的任务。

为了便于理解,我们可以将Callable比作一个Runnable接口,而Callable的call()方法则类似于Runnable的run()方法。

Callable的源码如下:

<span style="font-size:18px;">publi cinterface Callable<V> {    V call() throws Exception;}</span>

2. Future

Future 是一个接口。它用于表示异步计算的结果。提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果。

Future的源码如下:

<span style="font-size:18px;">public interface Future<V> {    // 试图取消对此任务的执行。    boolean     cancel(boolean mayInterruptIfRunning)//如果在任务正常完成前将其取消,则返回 true。    boolean     isCancelled()//如果任务已完成,则返回 true。    boolean     isDone()//如有必要,等待计算完成,然后获取其结果。    V           get() throws InterruptedException,ExecutionException;//如有必要,最多等待为使计算完成所给定的时间之后,获取其结果(如果结果可用)。    V             get(long timeout, TimeUnitunit)          throws InterruptedException,ExecutionException, TimeoutException;}</span>

 

示例的Callable和Future的基本用法

我们先通过一个示例看看Callable和Future的基本用法

<span style="font-size:18px;">importjava.util.concurrent.Callable;importjava.util.concurrent.Future;importjava.util.concurrent.Executors;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.ExecutionException; classMyCallable implements Callable {     @Override    public Integer call() throws Exception {        int sum    = 0;        // 执行任务        for (int i=0; i<100; i++)            sum += i;        //return sum;        return Integer.valueOf(sum);    }} publicclass CallableTest1 {     public static void main(String[] args)        throws ExecutionException,InterruptedException{        //创建一个线程池        ExecutorService pool =Executors.newSingleThreadExecutor();        //创建有返回值的任务        Callable c1 = new MyCallable();        //执行任务并获取Future对象        Future f1 = pool.submit(c1);        // 输出结果        System.out.println(f1.get());        //关闭线程池        pool.shutdown();    }}</span>


运行结果

4950

 

结果说明

在主线程main中,通过newSingleThreadExecutor()新建一个线程池。接着创建Callable对象c1,然后再通过pool.submit(c1)将c1提交到线程池中进行处理,并且将返回的结果保存到Future对象f1中。然后,我们通过f1.get()获取Callable中保存的结果;最后通过pool.shutdown()关闭线程池。

 

回到主题:调用查询手机号归属地的webservice

其实通过上面的简单例子,完全可以将通过Runnable接口或者Thread类实现的线程代码,修改成Callable和Future实现的线程。

<span style="font-size:18px;">public class Main Activity extends Activity {      public static final String TAG ="webService_pj";       private EditText phoneSecEditText;     private TextView resultView;     private Button queryButton;      @Override     public void onCreate(BundlesavedInstanceState) {            //           StrictMode.setThreadPolicy(newStrictMode.ThreadPolicy.Builder()//       .detectDiskReads().detectDiskWrites().detectNetwork()//        .penaltyLog().build());//           //    StrictMode.setVmPolicy(newStrictMode.VmPolicy.Builder()//       .detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()//        .build());          super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          phoneSecEditText = (EditText)findViewById(R.id.phone_sec);         resultView = (TextView)findViewById(R.id.result_text);         queryButton = (Button)findViewById(R.id.query_btn);          queryButton.setOnClickListener(newOnClickListener() {             @Override             public void onClick(View v) {                             Log.i(TAG,"MainActivity线程ID:"+Thread.currentThread().getId());                 // 手机号码(段)                 String phoneSec =phoneSecEditText.getText().toString().trim();                 // 简单判断用户输入的手机号码(段)是否合法                 if("".equals(phoneSec) || phoneSec.length() < 7) {                     // 给出错误提示                    phoneSecEditText.setError("您输入的手机号码(段)有误!");                    phoneSecEditText.requestFocus();                     // 将显示查询结果的TextView清空                    resultView.setText("");                     return;                 }                                // 命名空间                 String nameSpace = "http://WebXml.com.cn/";                 // 调用的方法名称                 String methodName ="getMobileCodeInfo";                 // EndPoint                 String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";                 // SOAP Action                 String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";                // method params and values                ArrayList<String> params= new ArrayList<String>();                ArrayList<Object> vals =new ArrayList<Object>();               params.add("mobileCode");                params.add("userId");                vals.add(phoneSec);                vals.add("");                                              //通过Callable和Future创建线程,调用WebService//创建有返回值的任务                CallableThread callable = newCallableThread(nameSpace,methodName,endPoint,soapAction,params,vals);              // 创建一个线程池                   ExecutorServiceexecutor=Executors.newCachedThreadPool();                                         //执行任务并获取Future对象                   Future<String>future = executor.submit(callable);                               try {//输出结果resultView.setText(future.get());  //关闭线程池executor.shutdown();}catch (InterruptedException e) {e.printStackTrace();}catch (ExecutionException e) {e.printStackTrace();}                           }         });     }       private class CallableThread implementsCallable<String> {         private String nameSpace;        private String methodName;        private String endPoint;        private String soapAction;        private ArrayList<String> params;        private ArrayList<Object> vals;         public CallableThread(StringnameSpace,  String methodName,                       StringendPoint, String soapAction, ArrayList<String> params,ArrayList<Object> vals){             this.nameSpace = nameSpace;            this.methodName = methodName;            this.endPoint = endPoint;            this.soapAction = soapAction;            this.params = params;            this.vals = vals;        }                //需要实现Callable的Call方法         public String call() throws Exception{ // 这个方法的实现见上篇文章或者源码          return getRemoteInfo(nameSpace,methodName, endPoint,                                soapAction,params,vals);        }     } </span>

   

至此,Android调用Webservice就完美的完成了。


源码下载

http://download.csdn.net/detail/tcl_6666/7365341


3 0