I have a class that builds a HttpResponse initializer. in one of the methods that should return the BasicNameValuePair
I have to check if there is a entry in the list with key or name specified by String "name".
public List<BasicNameValuePair> getPostPairs() {
if(mPostPairs == null || mPostPairs.size() < 1) {
throw new NullPointerException(TAG + ": PostPairs is null or has no items in it!");
}
//there is no hasName() or hasKey() method :(
if(!mPostPairs.hasName("action")) {
throw new IllegalArgumentException(TAG + ": There is no 'action' defined in the collections");
}
return mPostPairs;
}
How to do this? if it is not possible with BasicNameValuePair, what would be the alternative? subclassing and adding the method?
I need to use this for a HttpPost, which its setEntity only accepts this type:
public UrlEncodedFormEntity (List<? extends NameValuePair> parameters)
It seems that mPostPairs
is a List<BasicNameValuePair>
, and a list dont know what kind of objects are stored, you can iterate over it and check
boolean finded = false;
for (BasicNameValuePair pair : mPostPairs) {
if (pair.getName().equals("action")) {
finded = true;
break;
}
}
if (finded)
return mPostPairs;
else
throw new IllegalArgumentException(TAG + ": There is no 'action' defined in the collections");
Or shorter:
for (BasicNameValuePair pair : mPostPairs)
if (pair.getName().equals("action"))
return mPostPairs;
throw new IllegalArgumentException(TAG + ": There is no 'action' defined in the collections");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.