0

HttpResponse 初期化子を構築するクラスがあります。を返すメソッドの 1 つで、BasicNameValuePair文字列 "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;
}

これを行う方法?BasicNameValuePair でそれが不可能な場合、代替手段は何ですか? メソッドをサブクラス化して追加しますか?

setEntity がこのタイプのみを受け入れる HttpPost にこれを使用する必要があります。

public UrlEncodedFormEntity (List<? extends NameValuePair> parameters)
4

1 に答える 1

2

のようで、リストmPostPairsList<BasicNameValuePair>どのような種類のオブジェクトが保存されているかわかりません。それを繰り返して確認できます

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");

または短い:

for (BasicNameValuePair pair : mPostPairs) 
    if (pair.getName().equals("action")) 
        return mPostPairs;
throw new IllegalArgumentException(TAG + ": There is no 'action' defined in the collections");
于 2014-08-13T00:16:08.853 に答える