http——StringEntity 和 UrlEncodedFormEntity 的区别

1. UrlEncodedFormEntity() 

List<NameValuePair> pairs = new ArrayList<NameValuePair>();

NameValuePair pair1 = new BasicNameValuePair("supervisor", supervisorEt.getEditableText().toString());
NameValuePair pair2 = new BasicNameValuePair("content", superviseContentEt.getEditableText().toString());
NameValuePair pair3 = new BasicNameValuePair("userId", String.valueOf(signedUser.getId()));

pairs.add(pair1);
pairs.add(pair2);
pairs.add(pair3);

httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)) 

2. StringEntity()

JSONObject postData = new JSONObject();

postData.put("supervisor", supervisorEt.getEditableText().toString());
postData.put("content", superviseContentEt.getEditableText().toString());
postData.put("userId", signedUser.getId());

httpPost.setEntity(new StringEntity(postData.toString(), HTTP.UTF_8)); 

可以看出,UrlEncodedFormEntity()的形式比较单一,只能是普通的键值对,局限性相对较大。
而StringEntity()的形式比较自由,只要是字符串放进去,不论格式都可以。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页