2016-02-04 71 views
0

我正在研究一个项目,我尝试实现一个ASyncTask以发布到Web服务器。将ArrayList <NameValuePair>传递给ASyncTask和更新值

我有我的类定义如下:

public class PostToAPI extends AsyncTask<List<NameValuePair>, Void, JSONObject> implements DialogInterface.OnCancelListener 

我有我的doInBackground方法定义如下:

protected JSONObject doInBackground(List<NameValuePair>... postData) 

我想创建一个新的ArrayList,其中将包括在POSTDATA一切加一些额外的值,但我不断收到错误。

下面是我已经试过

protected JSONObject doInBackground(List<NameValuePair>... postData) 
{ 
    List<NameValuePair> updatedPostData = new ArrayList<>(); 
    updatedPostData.addAll(postData); 
updatedPostData.add(new BasicNameValuePair(Defines.PreferenceSettings.ACCESS_TOKEN, 
         settings.getString(Defines.PreferenceSettings.ACCESS_TOKEN, ""))); 
       updatedPostData.add(new BasicNameValuePair(Defines.PreferenceSettings.USER_ID, 
         String.valueOf(settings.getInt(Defines.PreferenceSettings.USER_ID, 0)))); 
} 

我在updatedPostData.addAll(postData)得到一个错误。错误是: 列表中的addAll(java.util.Collection?extends org.apache.http.NameValuePair>)无法应用于(java.util.List [])

现在,看起来像我一样doInBackground是传递一个我不想要的List数组。

感谢您的帮助,您可以提供

回答

3

由于在postData只有一个元素是,只需添加一个标这样的:

updatedPostData.addAll(postData[0]); 
-1

你的可变参数参数List<NameValuePair>... postDataList<NameValuePair>数组。

尝试protected JSONObject doInBackground(List<NameValuePair> postData)

+0

这是行不通的,因为'AsyncTask'需要'doInBackground()'采用可变参数。 –

相关问题