如何在执行apache HttpPost时在UrlEncodedFormEntity中将空格编码为%20?

rDr*_*oid 3 apache encoding android httpclient url-encoding

我正在点击的网络服务需要参数为URLEncodedFormEntity.我无法根据Web服务的要求将空间更改为%20,而是将空间转换为+.

我的代码是:

HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
            HTTP.UTF_8);
post.setEntity(entity);
HttpResponse resp = client.execute(post);
Run Code Online (Sandbox Code Playgroud)

其中参数是List<NameValuePair>参数.

我阅读了很多帖子,所有帖子都建议mancall在emcoding之后将空间更改为%20.在这里,我如何访问实体并手动更改它?任何帮助将不胜感激.

Jen*_*ens 8

UrlEncodedFormEntity基本上是一个带有自定义构造函数的StringEntity,您实际上不必使用它来创建可用的实体.

String entityValue = URLEncodedUtils.format(parameters, HTTP.UTF_8);
// Do your replacement here in entityValue
StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
// And now do your posting of this entity
Run Code Online (Sandbox Code Playgroud)

  • 尝试向参数添加带有"itemName"和"Potato Chips"的新BasicNameValuePair,然后执行entityValue.replaceAll("\\ +","%20"); 我写的地方//在entityValue中替换你的. (2认同)