NameValuePair在API22过时问题

在更高版本的编译环境中,如果使用NameValuePair的话会出现这样的提示:The type NameValuePair is deprecated,也就是说这个类以及过时了。

既然过时了,那么有什么方法替换这个API呢?经查阅有一个方法比较合适:

首先是如何拼接键值对。这里我们使用的是ContentValues

ContentValues content = new ContentValues();
content.put("sign", "test");
content.put("userId", "0");
content.put("page",1 + "");
content.put("size", "20");
content.put("sort", "");
content.put("types", "news");

就这样的方法完全可以替换NameValuePair。那么说到这里也许就下一个疑问来了,怎么获取对应的键值对。要知道NameValuePair是有getName(),getValue()的人,而你ContentValues要怎么样去便利获取键值对呢??详细的可以看看以下代码

for (Map.Entry<String, Object> entry : content.valueSet()) {
LogUtils.d("键:"+entry.getKey()+",值:"+entry.getValue().toString());
}

通过一个for循环,遍历content中的值,转化为Map.Entry<T,T>类型,就能通过getKey()和getValue()获取对应的键值对了。

原文链接:https://blog.csdn.net/u014722744/article/details/50864092

posted @ 2021-10-21 14:26  似水流云  阅读(139)  评论(0编辑  收藏  举报