Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "String... params" mean if passed as a parameter? [duplicate]

Tags:

java

android

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...
    }
}
like image 461
Ali Almohsen Avatar asked Jun 29 '13 16:06

Ali Almohsen


People also ask

What is string params?

On the internet, a Query string is the part of a link (otherwise known as a hyperlink or a uniform resource locator, URL for short) which assigns values to specified attributes (known as keys or parameters).

What does params mean in Java?

Parameters. A parameter is a variable used to define a particular value during a function definition. Whenever we define a function we introduce our compiler with some variables that are being used in the running of that function.

When a parameter is passed to the method it is called an?

When a parameter is passed to the method, it is called an argument.

Can we pass string as parameter in Java?

String objects are immutable in Java; a method that is passed a reference to a String object cannot change the original object.


3 Answers

the three dot stays for vargars. you can access it like a String[].

If a method takes as paramter a varargs, you can call it with multiple values for the vargars type:

public void myMethod(String... values) {}

you can call like myMethod("a", "b");

in myMethod values[0] is equals "a" and values[1] is equals to "b". If you have a method with multiple args, the vargars argument has to be the last: for instance:

public void myMethod(int first, double second, String... values) {}
like image 100
Blackbelt Avatar answered Oct 16 '22 02:10

Blackbelt


 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...)

The parameters of the asynchronous task are passed to doInBackground

like image 35
Raghunandan Avatar answered Oct 16 '22 03:10

Raghunandan


From the javadocs:

public static String format(String pattern,
                                Object... arguments);

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

like image 34
Juned Ahsan Avatar answered Oct 16 '22 02:10

Juned Ahsan