Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 使用线程和异步任务_Multithreading_Android Asynctask - Fatal编程技术网

Multithreading 使用线程和异步任务

Multithreading 使用线程和异步任务,multithreading,android-asynctask,Multithreading,Android Asynctask,我是android新手,在异步任务和线程方面有问题。 如何在此代码中使用AsyncTask? 当我像这样使用productIdList时,它是空的。这就是为什么我要使用AsyncTask。我认为使用AsyncTask可以工作 提前谢谢 public ArrayList<String> getProductData() { final ArrayList<String> productIdList = new ArrayList<String>();

我是android新手,在异步任务和线程方面有问题。 如何在此代码中使用AsyncTask? 当我像这样使用productIdList时,它是空的。这就是为什么我要使用AsyncTask。我认为使用AsyncTask可以工作

提前谢谢

public ArrayList<String> getProductData() {


    final ArrayList<String> productIdList = new ArrayList<String>();

    new Thread(new Runnable() {

        public void run() {
            HttpClient httpclient= new DefaultHttpClient();
            GeneralConstans GC = new GeneralConstans();
            // Products will be stated in memory
            HttpPost httpget = new HttpPost(GC.UrlConstants);
            HttpResponse response;
            String result = null;
            try {

                HttpContext ctx = new BasicHttpContext();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                        "UTF-8"));

                response = httpclient.execute(httpget, ctx);
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity);
                    JSONArray arr = new JSONArray(result);
                    Gson gson = new Gson();
                    if (arr.length() > 0) {
                        for (int j = 0; j < arr.length(); j++) {
                            Product p = gson.fromJson(arr.getString(j),
                                    Product.class);
                            productIdList.add(p.toString());

                        }                       

                    }

                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                /*if (checkAbortStatus(e.getMessage()) == true) {
                    handler.sendEmptyMessage(0);
                }*/
            } catch (IOException e) {
                /*if (checkAbortStatus(e.getMessage()) == true) {
                    handler.sendEmptyMessage(0);
                }*/
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                super.handleMessage(msg);
            }
        }; 

    }).start();
    return productIdList;
public ArrayList getProductData(){
最终ArrayList productIdList=新ArrayList();
新线程(newrunnable()){
公开募捐{
HttpClient HttpClient=新的DefaultHttpClient();
GeneralConstans GC=新的GeneralConstans();
//产品将在内存中声明
HttpPost httpget=新的HttpPost(GC.urlstants);
HttpResponse响应;
字符串结果=null;
试一试{
HttpContext ctx=新的BasicHttpContext();
List name valuepairs=new ArrayList(
2);
httpget.setEntity(新的UrlEncodedFormEntity(nameValuePairs,
“UTF-8”);
response=httpclient.execute(httpget,ctx);
HttpEntity当前性=response.getEntity();
if(最近性!=null){
结果=EntityUtils.toString(最近性);
JSONArray arr=新JSONArray(结果);
Gson Gson=新的Gson();
如果(阵列长度()>0){
对于(int j=0;j
异步任务隐式地将方法和命令从主线程移开,因为主线程应该运行所有任务

创建一个新类

public class <NAME OF CLASS> extends AsyncTask<Void, String, String>
public类扩展异步任务
extends部分基本上扩展(在c#中继承)了一些参数,这些参数与您将要使用的3个重写方法相关

  • onPreExecute——这是一种预处理方法,我个人不需要我编写的代码(我自己对android还是新手)

  • onDoInBackgound-这是AsyncTask的主要部分,这是所有方法将要去的地方,这是所有逻辑将发生的地方。这正是它在tin上所说的,它在另一个线程的后台执行所有操作

  • onPostExecute-当onDoInBackground完成时,它将运行onPostExecute方法,我通常在onDoInBackground方法上有一个字符串返回,这确保它进展到onPostExecute,因为我发现有时没有它,它不会有很大进展

  • 然后在postExecute方法中,您告诉它在完成所有逻辑后要做什么,例如,您可以在主线程上有一个侦听器,您可以在其中从AYSNTASK调用该侦听器,即postExecute方法中的listener.onSuccess(results),它将返回到原始线程

    希望这有帮助