N
N
Nur-Magomed2014-05-01 18:49:23
Java
Nur-Magomed, 2014-05-01 18:49:23

How to pass a variable (List) when subclassing AsyncTask in Android?

I recently started programming for Android. Faced a problem. First, I wrote the class code without passing the value (works fine, eclipse + ADT development environment):

First version of the code
private class PostDataSendClass extends AsyncTask<Void,Void,Void> {
   protected Void doInBackground(Void... paramsf) {
      	 HttpClient httpclient = new DefaultHttpClient();
   		HttpPost httppost = new HttpPost("http://site.com/");
   		httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
   		httppost.setHeader("Accept", "application/json");

   		List<NameValuePair> params = new ArrayList<NameValuePair>(2);
   		params.add(new BasicNameValuePair("act", "getToken"));
   		params.add(new BasicNameValuePair("hash", "12345"));

   		try {
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

   		HttpResponse response = null;
      try {
        response = httpclient.execute(httppost);
      } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
   		HttpEntity entity = response.getEntity();
          
   		try {
        Log.d("BAR","Ans:"+EntityUtils.toString(entity,"UTF-8"));
      } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
 
      	 return null;
       }
   }

//вызов при нажатии на кнопку
public void post_Click(View v){
  Log.d("BAR","POst_test_click");
  new PostDataSendClass().execute();
}


Then the List params, which is created in the class above, in the second option, I tried to pass when creating the class (when the button is pressed), but I don’t know which type to apply, in any case, a type error occurs or the environment issues errors:
The second version of the code
private class PostDataSendClass extends AsyncTask<List<NameValuePair>,Void,Void> {	
   protected Void doInBackground(List<NameValuePair>... paramsto) {
      	 HttpClient httpclient = new DefaultHttpClient();
   		HttpPost httppost = new HttpPost("http://site.com/");
   		httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
   		httppost.setHeader("Accept", "application/json");
   		
   		try {
/*На след. строчку "ругается" среда, а именно ошибка:
 <<The constructor UrlEncodedFormEntity(List<NameValuePair>[], String) is undefined>>*/
            httppost.setEntity(new UrlEncodedFormEntity(paramsto, "UTF-8")); 
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

   		HttpResponse response = null;
      try {
        response = httpclient.execute(httppost);
      } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
   		HttpEntity entity = response.getEntity();
          
   		try {
        Log.d("BAR","Ans:"+EntityUtils.toString(entity,"UTF-8"));
      } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
 
      	 return null;
       }
   }

//вызов
public void post_Click(View v){
    Log.d("BAR","Post_test_click");
    List<NameValuePair> vars = new ArrayList<NameValuePair>(2);
    vars.add(new BasicNameValuePair("act", "getToken"));
    vars.add(new BasicNameValuePair("hash", "12345"));
    new PostDataSendClass().execute(vars); //здесь ошибка: Type safety: A generic array of 
//List<NameValuePair> is created for a varargs parameter
    }


Please tell me how to organize this business.
PS In the title of the question, Toaster removed the angle brackets and left the List insteadList<NameValuePair>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anyd3v, 2014-05-01
@efive

Because the UrlEncodedFormEntity constructor:
public UrlEncodedFormEntity(java.util.List<? extends org.apache.http.NameValuePair> parameters, java.lang.String compiled encoding) throws java.io.UnsupportedEncodingException { /* code */ }
At that time as paramsto is a wildcard argument (think of a collection), you need to take an element of this collection (those are paramsto[0]) so
as a result
httppost.setEntity(new UrlEncodedFormEntity(paramsto[0], "UTF-8")
you would do well to read about Java

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question