Answer the question
In order to leave comments, you need to log in
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):
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();
}
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
}
List<NameValuePair>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question