发表于: 2017-11-19 21:01:09
2 624
今天做的事:
查了一下昨天遗留的问题
NameValuePair
NameValuePair是简单名称节点类型,主要表达形式是键值对的表达形式。
介绍的资料少之又少,所以只能通过一面之词,外加自己的推测,感觉确实差不多。
Map.Entry
Map的entrySet()方法返回一个实现Map.Entry接口的对象集合。集合中每个对象都是底层Map中一个特定的键/值对。
通过这个集合的迭代器,您可以获得每一个条目(唯一获取方式)的键或值并对值进行更改。当条目通过迭代器返回后,除非是迭代器自身的remove()方法或者迭代器返回的条目的setValue()方法,其余对源Map外部的修改都会导致此条目集变得无效,同时产生条目行为未定义。
网上的介绍也就只有这些,基本介绍的都是这个内容,我的理解就是
取出的是这个map中某一个特定的键值对。
而
map.entrySet()
方法取出来的是map中特定的键值对的内容组成的一个集合。
这里刚好和之前的NameValuePair契合上了。
最后的
UrlEncodedFormEntity
干脆就是一个编码转换
UrlEncodedFormEntity这个类是用来把输入数据编码成合适的内容
所以这些内容其实并没有什么东西。
感觉不开心,那看看源码吧
public class BasicNameValuePair implements NameValuePair, Cloneable, Serializable {
private static final long serialVersionUID = -6437800749411518984L;
private final String name;
private final String value;
public BasicNameValuePair(String name, String value) {
this.name = (String)Args.notNull(name, "Name");
this.value = value;
}
public String getName() {
return this.name;
}
public String getValue() {
return this.value;
}
public String toString() {
if(this.value == null) {
return this.name;
} else {
int len = this.name.length() + 1 + this.value.length();
StringBuilder buffer = new StringBuilder(len);
buffer.append(this.name);
buffer.append("=");
buffer.append(this.value);
return buffer.toString();
}
}
public boolean equals(Object object) {
if(this == object) {
return true;
} else if(!(object instanceof NameValuePair)) {
return false;
} else {
BasicNameValuePair that = (BasicNameValuePair)object;
return this.name.equals(that.name) && LangUtils.equals(this.value, that.value);
}
}
public int hashCode() {
int hash = 17;
int hash = LangUtils.hashCode(hash, this.name);
hash = LangUtils.hashCode(hash, this.value);
return hash;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
可以看到这个源码,内容只有key和value,所以确实是键值对形式。
并且重写了toString、equals、hashCode方法。但是还是没办法掩盖它是一个键值对形式的对象这个事实。
再看下一个
/**
* A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns
* a collection-view of the map, whose elements are of this class. The
* <i>only</i> way to obtain a reference to a map entry is from the
* iterator of this collection-view. These <tt>Map.Entry</tt> objects are
* valid <i>only</i> for the duration of the iteration; more formally,
* the behavior of a map entry is undefined if the backing map has been
* modified after the entry was returned by the iterator, except through
* the <tt>setValue</tt> operation on the map entry.
*
* @see Map#entrySet()
* @since 1.2
*/
源码中的解释,看一看到,和之前贴过来的内容基本没有出入,就是取出map.class中的内容,然后放到集合中,并且只能用迭代器
iteration
进行操作等等。
不过奇怪的是,我的POST方法中并没有使用迭代器。。。
而且实现这个接口的实现类还挺多的。。。
而而且,这个是一个接口内的接口,第一次见,长知识(Map接口内的)
最后的源码
public class UrlEncodedFormEntity extends StringEntity {
public UrlEncodedFormEntity(List<? extends NameValuePair> parameters, String charset) throws UnsupportedEncodingException {
super(URLEncodedUtils.format(parameters, charset != null?charset:HTTP.DEF_CONTENT_CHARSET.name()), ContentType.create("application/x-www-form-urlencoded", charset));
}
public UrlEncodedFormEntity(Iterable<? extends NameValuePair> parameters, Charset charset) {
super(URLEncodedUtils.format(parameters, charset != null?charset:HTTP.DEF_CONTENT_CHARSET), ContentType.create("application/x-www-form-urlencoded", charset));
}
public UrlEncodedFormEntity(List<? extends NameValuePair> parameters) throws UnsupportedEncodingException {
this((Iterable)parameters, (Charset)((Charset)null));
}
public UrlEncodedFormEntity(Iterable<? extends NameValuePair> parameters) {
this((Iterable)parameters, (Charset)null);
}
}
继承自StringEntity
可以看出就是处理字符相关的,比如编码什么的
DEF_CONTENT_CHARSET = Consts.ISO_8859_1;
public static ContentType create(String mimeType, Charset charset) {
String type = ((String)Args.notBlank(mimeType, "MIME type")).toLowerCase(Locale.US);
Args.check(valid(type), "MIME type may not contain reserved characters");
return new ContentType(type, charset);
}
public static String format(Iterable<? extends NameValuePair> parameters, Charset charset) {
return format(parameters, '&', charset);
}
看源码跳来跳去的,真有意思,一个也看不懂(哈哈哈哈)
反正理解成编码就行了。
今天就水到这了。
明天计划:继续刚文件上传,微信接口
问题:无
收获:看了昨天没理解的内容,其实内容也不多,大概的扫一眼源码,不求甚解,ok了~
评论