螺纹Web服务调用 [英] Threaded web service call

查看:144
本文介绍了螺纹Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我请求我的Andr​​oid应用程序从Web服务的信息。 Web服务返回的项目列表,有一个base64形象和一些信息的每个项目

I am requesting information from a web service in my android app. The web service returns a list of items, each item having a base64 image and some info

第一种方法:简单地访问Web服务和得到的结果。这意味着用户界面的冻结,直到数据被下载。没有一个很好的解决方案。

First method: simply access the web service and get the result. This means freeze of UI until data is downloaded. Not a good solution

第二种方法:把数据下载在一个线程中,并显示一条消息,一个进度条。

Second method: put the data download in a thread, and display a progress bar with a message.

 private void StartGettingData()
    {
        viewInfo = new Runnable(){
            public void run() {
                getData(); //from the web service
            }
        };
        Thread thread =  new Thread(null, viewInfo, "MagentoBackground");
        thread.start();
        progressDialog = ProgressDialog.show(TracksListActivity.this,    
              "Please wait...", "Retrieving data from WEB...", true);
    }

    private void getData(){
    {
      get data from web service into a list and then 

      runOnUiThread(returnRes);
    }

     private Runnable returnRes = new Runnable() {

     public void run() {
         populate the listview adapter with info from the web service result list and

         progressDialog.dismiss();
         generalTrackInfoAdapter.notifyDataSetChanged();
     }
 };

这显示了一个很好的加载图像和信息。用户将不得不等待,直到全部下载完毕。我不知道如何取消的getData()线程。

This shows a nice loading image and the message. The user will have to wait until all the download is complete. I don't know how to cancel the getdata() thread.

第三种方法:我想有一些像,用户presses一个按钮,通过Web服务项目来获得数据,一个线程下载项目,并立即显示在列表中。利用可随时取消了按键preSS的要求。

Third method: I would like to have something like, the user presses a button to get data, a thread downloads item by item from the web service and immediate shows it in the list. The use can always cancel the request with a button press.

但是,如何做到这一点?或者有另一种方式?

But, how to do this ? Or is there another way ?

推荐答案

您可以使用的AsyncTask 作为穿线,基础知识:的无痛线程,例如:的对于多线程性能(图片下载)

You can use AsyncTask for threading, basics: painless threading, example:Multithreading For Performance(image downloader).

由于使用Thread类或Runnable接口的code变得更为复杂和难以阅读。它成为当你实现需要频繁更新的用户界面复杂的操作,更是雪上加霜。

Because using the class thread or the interface runnable your code becomes more complicated and more difficult to read. It becomes even worse when you implement complex operations that require frequent UI updates.

的AsyncTask的目标是照顾的线程管理的给你。

The goal of AsyncTask is to take care of thread management for you.

的基本结构是:

public class DownloaderTask extends AsyncTask<String, String, String>{

    protected void onPreExecute(){}

    protected String doInBackground(String....args){
        //do something in background
    }

    protected void onProgressUpdate(String str){}

    protected void onPostExecute(String result){
         //..update ui
    }
}

......开始你的任务,例如:

...to start your task e.g.:

public void onClick(View v){
     new DownloaderTask().execute("param");
}

  • 该方法doInBackground()执行 自动在一个工作线程

    • The method doInBackground() executes automatically on a worker thread

      在preExecute(),onPostExecute()和 onProgressUpdate()都调用上 在UI线程

      onPreExecute(), onPostExecute() and onProgressUpdate() are all invoked on the UI thread

      返回的值由 doInBackground()被发送到 onPostExecute()

      The value returned by doInBackground() is sent to onPostExecute()

      您可以在调用publishProgress() 随时在doInBackground()来 在UI上执行onProgressUpdate() 螺纹

      You can call publishProgress() at anytime in doInBackground() to execute onProgressUpdate() on the UI thread

      您可以随时取消任务, 从任何线程

      You can cancel the task at any time, from any thread

      这篇关于螺纹Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆