Issue
I am working on a project where I am trying to implement an ASyncTask to post to a web server.
I have my class defined as follows:
public class PostToAPI extends AsyncTask<List<NameValuePair>, Void, JSONObject> implements DialogInterface.OnCancelListener
I have my doInBackground method defined as follows:
protected JSONObject doInBackground(List<NameValuePair>... postData)
I am trying to create a new ArrayList<NameValuePair>
which will include everything in postData plus some extra values but I keep getting an error.
Below is what I've tried
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))));
}
I'm getting an error on the updatedPostData.addAll(postData)
. The error is:
addAll (java.util.Collection? extends org.apache.http.NameValuePair>) in
List cannot be applied to (java.util.List<org.apache.http.NameValuePair>[])
Now that looks to me like doInBackground is passing an array of List which I don't want.
Solution
Since there is only one element in postData
, just add a subscript like this:
updatedPostData.addAll(postData[0]);
Answered By - Jim Rhodes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.