您的位置:首页 > 移动开发

post 请求 application/x-www-form-urlencoded。android 中文 乱码

2016-05-19 16:07 567 查看
/**
* 发送post请求
*/
public static Object post(RequestVo vo){
DefaultHttpClient client = new DefaultHttpClient();
//		LogUtil.w("URL:", vo.context.getString(R.string.app_host).concat(vo.context.getString(vo.requestUrl)));
HttpPost post = new HttpPost(vo.requestUrl);
HttpParams params = new BasicHttpParams();//
params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 25000);   //连接超时
HttpConnectionParams.setSoTimeout(params, 25000);   //响应超时
post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
Object obj = null;
try {
if(vo.requestDataMap!=null){
HashMap<String,String> map = vo.requestDataMap;

StringBuilder sb = new StringBuilder();
ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry:map.entrySet()){
BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pairList.add(pair);
//输出 请求结果 用于LogUtil 调试
sb.append(entry.getKey()).append("=").append(entry.getValue())
.append("&");
}
LogUtil.e(vo.requestUrl+ "?"+sb.toString() );
HttpEntity entity = new UrlEncodedFormEntity(pairList,"UTF-8");
post.setEntity(entity);
}
HttpResponse response = null;
try {
response = client.execute(post);//包含响应的状态和返回的结果==

} catch (IllegalArgumentException e) {
}

int responseCode = response.getStatusLine().getStatusCode();


原本在 请求post 的时候
new UrlEncodedFormEntity(pairList,"UTF-8"); 就 指定了编码格式 。
无奈 ,服务器接收到的的时候,还是乱码,返回的也是乱码 。

后来 有添加了 <pre name="code" class="java">post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
才搞好 。

应该和服务器开发 接口的有关系。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: