求助android客户端传回的汉字参数,在服务器端出现乱码
1个回答
展开全部
android,遇到从android客户端向服务器端发送汉字乱码问题。采用URLConnection的GET方式,在客户端和服
务端都需要进行转码,而采用POST方式则不需要转码。具体方法如下:
用URLConnection从android发送数据有两种方式:
第一种方式:采用get方式传值
(1)客户端代码:
URL url = new URL(mUrl);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = is.read()) != -1) {
baf.append((byte) current);
}
requestInfo = new String(baf.toByteArray(), "UTF-8").trim();
is.close();
对汉字进行处理:
URLEncoder.encode(URLEncoder.encode(channelName, "UTF-8"), "UTF-8")
(2)服务器端接收字段:
URLDecoder.decode(URLDecoder.decode(request.getParameter("nickname"), "UTF-8"), "UTF-8")
第二种方式:采用Post方式:
客户端代码:
public String sendRemoteRequest(String path,String param){
Log.i("lisheng", param.toString());
Log.i("lisheng", path);
String strRes="";
OutputStream os = null;
DataOutputStream dos = null;
InputStream is = null;
BufferedReader br = null;
try {
URL url = new URL(path);
URLConnection urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
os = urlConn.getOutputStream();
dos = new DataOutputStream(os);
dos.write(param.getBytes());
dos.flush();
dos.close();
os.close();
is = urlConn.getInputStream();
br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
for (String strLine = ""; (strLine = br.readLine()) != null;)
strRes = (new StringBuilder(String.valueOf(strRes))).append(strLine).toString();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strRes;
}
参数里直接写汉字
服务器端代码:
request.setCharacterEncoding("UTF-8");
request.getParameter("nickname")
即可得到参数为汉字的值,不需要转码。
务端都需要进行转码,而采用POST方式则不需要转码。具体方法如下:
用URLConnection从android发送数据有两种方式:
第一种方式:采用get方式传值
(1)客户端代码:
URL url = new URL(mUrl);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = is.read()) != -1) {
baf.append((byte) current);
}
requestInfo = new String(baf.toByteArray(), "UTF-8").trim();
is.close();
对汉字进行处理:
URLEncoder.encode(URLEncoder.encode(channelName, "UTF-8"), "UTF-8")
(2)服务器端接收字段:
URLDecoder.decode(URLDecoder.decode(request.getParameter("nickname"), "UTF-8"), "UTF-8")
第二种方式:采用Post方式:
客户端代码:
public String sendRemoteRequest(String path,String param){
Log.i("lisheng", param.toString());
Log.i("lisheng", path);
String strRes="";
OutputStream os = null;
DataOutputStream dos = null;
InputStream is = null;
BufferedReader br = null;
try {
URL url = new URL(path);
URLConnection urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
os = urlConn.getOutputStream();
dos = new DataOutputStream(os);
dos.write(param.getBytes());
dos.flush();
dos.close();
os.close();
is = urlConn.getInputStream();
br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
for (String strLine = ""; (strLine = br.readLine()) != null;)
strRes = (new StringBuilder(String.valueOf(strRes))).append(strLine).toString();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strRes;
}
参数里直接写汉字
服务器端代码:
request.setCharacterEncoding("UTF-8");
request.getParameter("nickname")
即可得到参数为汉字的值,不需要转码。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询