暂停主线程直到异步任务完成

时间:2016-05-23 03:18:04

标签: java android

我有一个异步任务,它创建一个将在之后立即使用的数据库(因此任务必须在继续之前完成)。我也不能使用postExecute,因为每次都不会创建数据库,并且使用get()方法似乎不起作用。

以下是执行异步任务的代码。

try {
    DatabaseCreator create = new DatabaseCreator();
    create.execute("http://api.nal.usda.gov/ndb/search/?format=xml&api_key="+foodDataKey + "&fg=0900&max=360",
                   "http://api.nal.usda.gov/ndb/search/?format=xml&api_key="+foodDataKey + "&fg=1100&max=836",
                   "http://api.nal.usda.gov/ndb/search/?format=xml&api_key="+foodDataKey + "&fg=0100&max=283").get();
} catch (Exception e) {

}

这是异步任务本身。

class DatabaseCreator extends AsyncTask<String, Void, ArrayList<ArrayList<String>>> {
    @Override
    protected ArrayList<ArrayList<String>> doInBackground(String... params) {
        ArrayList<ArrayList<String>> products = new ArrayList<ArrayList<String>>();
        try {
            for (int i = 0; i < params.length; i++) {
                URL connect = new URL(params[i]);
                URLConnection foodConnect = connect.openConnection();
                BufferedReader read = new BufferedReader(new InputStreamReader(foodConnect.getInputStream()));
                String input;
                ArrayList<String> currentGroup = new ArrayList<String>();
                if (i ==  0) {
                    currentGroup.add("Fruits");
                } else if (i == 1) {
                    currentGroup.add("Vegetables");
                } else {
                    currentGroup.add("Dairy");
                }
                while ((input = read.readLine()) != null){
                    if (input.contains("<name>")) {
                        if (input.trim().length() < 8) {
                            String temp = read.readLine().trim();
                            temp = temp.substring(0, temp.indexOf(","));
                            if (currentGroup.contains(temp) == false) {
                                currentGroup.add(temp);
                            }
                        } else {
                            String temp = input.trim();
                            //Log.d("HOME", temp + " " + temp.indexOf("<name>"));
                            temp = temp.substring(temp.indexOf("<name>") + 6, temp.indexOf("</name>"));
                            if (temp.indexOf(",") == -1) {
                                if (currentGroup.contains(temp) == false) {
                                    currentGroup.add(temp);
                                }
                            } else {
                                temp = temp.substring(0, temp.indexOf(","));
                                if (currentGroup.contains(temp) == false) {
                                    currentGroup.add(temp);
                                }
                            }
                        }
                    }
                }
                products.add(currentGroup);
            }
            return products;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException io) {
            io.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(ArrayList<ArrayList<String>> prods) {
        //Log.d("HOME", "HHIIHIH");
        for (int i = 0; i < prods.size(); i++) {
            //Log.d("HOME", prods.get(i)+"");
            database.add(prods.get(i));
        }
        //Log.d("HOMEEEE", database.size()+"");
    }
}

0 个答案:

没有答案