什么是“String ... params”是否作为参数传递? [英] What does "String... params" mean if passed as a parameter?

查看:2037
本文介绍了什么是“String ... params”是否作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了这个代码,有一部分我不明白。对于方法doInBackground,传递的参数是 String ... params 。有人可以向我解释这意味着什么?那是什么 ...

I found this code online and there's 1 part I don't understand. For the method doInBackground, the parameter passed is String... params. Could someone please explain to me what that means? What is that ...?

public class AsyncHttpPost extends AsyncTask<String, String, String> {
    private HashMap<String, String> mData = null;// post data

    /**
     * constructor
     */
    public AsyncHttpPost(HashMap<String, String> data) {
        mData = data;
    }

    /**
     * background
     */
    @Override
    protected String doInBackground(String... params) {
        byte[] result = null;
        String str = "";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL
        try {
            // set up post data
            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            Iterator<String> it = mData.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                nameValuePair.add(new BasicNameValuePair(key, mData.get(key)));
            }

            post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
                result = EntityUtils.toByteArray(response.getEntity());
                str = new String(result, "UTF-8");
            }
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
        }
        return str;
    }

    /**
     * on getting result
     */
    @Override
    protected void onPostExecute(String result) {
        // something...
    }
}


推荐答案

 doInBackground(String... params)
 // params represents a vararg.
 new AsyncHttpPost().execute(s1,s2,s3); // pass strings to doInbackground
 params[0] is the first string
 params[1]  is the second string 
 params[2]  is the third string 

http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground(Params ...)

异步任务的参数传递给 doInBackground

The parameters of the asynchronous task are passed to doInBackground

这篇关于什么是“String ... params”是否作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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