【问题标题】:Why String... urls is used as an argument in `doInBackgroud()` method? [duplicate]为什么 String... urls 用作 `doInBackgroud()` 方法中的参数? [复制]
【发布时间】:2015-04-30 11:28:31
【问题描述】:

我已经实现了从 web 服务获取 InputStream 的 android 代码,为此我使用了AsyncTask。但我无法理解doInBackground() 方法中参数“String... urls”的用途。有人可以解释一下吗?以下是我的代码:

private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            urlText.setText("");
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

【问题讨论】:

    标签: android android-asynctask android-networking


    【解决方案1】:

    不过是Variable Argument (Varargs): in java

    所以你可以通过multiple arguments in an array

    例子

    public class HelloWorldVarargs {
    
    public static void main(String args[]) {
        test(215, "India", "Delhi");
        test(147, "United States", "New York", "California");
    }
    
    public static void test(int some, String... args) {
        System.out.print("\n" + some);
        for(String arg: args) {
            System.out.print(", " + arg);
        }
      }
    }
    

    【讨论】:

    • 好的,我明白了.. 谢谢唐
    猜你喜欢
    • 2018-05-09
    • 2019-02-12
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2021-12-03
    • 2015-10-23
    • 2016-12-05
    • 2013-06-27
    相关资源
    最近更新 更多