android异步任务返回类型和锁定

时间:2015-11-14 01:23:01

标签: android android-asynctask

我实现了以下assynctask。它的用法非常简单,因此到目前为止一直有效。获取一个网址,发布到它,获取其内容,将它们写入文件。困难的部分现在开始

问题:  对于多个不同的文件,我需要多次重复使用这段代码。如何将文件作为变量传递给assynctask调用以及url?

//class to call a url and save it to a local file
        private class url_to_file extends AsyncTask<String, Integer, String> {

            protected String[] doInBackground(String... input) {
                //function to call url and postback contents
                return callpost(input[0]);
            }

            protected void onProgressUpdate(Integer... progress) {
                //Yet to code
            }

            protected void onPostExecute(String result) {
                //function to write content to text file
                writeStringAsFile( result, "file.xml" ,getApplicationContext());

            }
        }

编辑: 纯粹作为参考,我用来读取,写入文件和调用url的函数

//saves a txt (etc, xml as well) file to directory,replacing previous. if directory is left empty, save to assets
    public static void writeStringAsFile(final String fileContents, String fileName ,Context context) {
        try {
            FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));
            out.write(fileContents);
            out.close();
        } catch (IOException e) {
        }
    }

    //read file, returns its contents
    public static String readFileAsString(String fileName,Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        BufferedReader in = null;

        try {
            in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
            while ((line = in.readLine()) != null) stringBuilder.append(line);

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

        return stringBuilder.toString();
    }

    //calls a page. Returns its contents
    public String callpost (String... strings)
    {
        StringBuilder content = new StringBuilder();
        try
        {
            // create a url object
            URL url = new URL(strings[0]);

            // create a urlconnection object
            URLConnection urlConnection = url.openConnection();

            // wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            String line;

            // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null)
            {
                content.append(line + "\n");
            }
            bufferedReader.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return content.toString();
    }

编辑: 删除了第二个问题,因为它与其余部分无关,只会让人们混淆看线程

1 个答案:

答案 0 :(得分:0)

Kudos @FirstOne对评论的帮助

这为我做了

nodeHandlers.Add("li", (node, parent) =>
{
    var listStyle = node.ParentNode.Name == "ul"
        ? "UnorderedList"
        : "OrderedList";

    var section = (Section)parent;
    var isFirst = node.ParentNode.Elements("li").First() == node;
    var isLast = node.ParentNode.Elements("li").Last() == node;

    var listItem = section.AddParagraph().SetStyle(listStyle);

    if (listStyle == "UnorderedList")
    {
        listItem.Format.ListInfo.ListType = _nestedListLevel%2 == 1 ? ListType.BulletList2 : ListType.BulletList1;
    }
    else
    {
        listItem.Format.ListInfo.ListType = _nestedListLevel % 2 == 1 ? ListType.NumberList2 : ListType.NumberList1;
    }

    if (_nestedListLevel > 0)
    {
        listItem.Format.LeftIndent = String.Format(CultureInfo.InvariantCulture, "{0}in", _nestedListLevel*.75);
    }

    // disable continuation if this is the first list item
    listItem.Format.ListInfo.ContinuePreviousList = !isFirst;

    if (isLast)
        _nestedListLevel--;

    return listItem;
});