使用android手机,在使用post请求传递中文内容向服务器端时,如果服务器端要求使用utf-8的编码格式时需要对post请求数据做以下处理;
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("get_uid",
get_uid));
nameValuePair.add(new BasicNameValuePair("message",
message));
Log.i("cat", message);
nameValuePair.add(new BasicNameValuePair("calendarlist[pageindex]",
pageindex));
nameValuePair.add(new BasicNameValuePair("calendarlist[recordlimit]",
recordlimit));httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
这里的message字段是含有中文的;而一般的字符转换方法如:new String(message.getBytes(), "UTF-8 ");这样转换,是不成功的;
这里需要用到的方法就是new UrlEncodedFormEntity(nameValuePair,"UTF-8");
查看UrlEncodedFormEntity的api,有两种方法:
普通情况下是不做转换,使用第二种方法:
)">UrlEncodedFormEntity(List<? extends NameValuePair> parameters);
如果做转换编码的话,就要使用第一种方法:
, java.lang.String)">UrlEncodedFormEntity(List<? extends NameValuePair> parameters, String encoding)
设置encoding;例如"UTF-8";