安卓经常使用http与服务器交互,但是如果提交的数据有中文,就经常出现乱码。
1.首先将workbench编码改成utf-8
2.将请求的url中的英文进行转码
3.拼接最终url,并请求
String test=URLEncoder.encode("罗占伟", "UTF-8"); String urlstring="http://luozhanwei1.app.com/bee/getuserdata.php?realname="+test; URL url=new URL(urlstring); HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection(); InputStreamReader isr=new InputStreamReader(urlConnection.getInputStream(),"UTF-8" ); BufferedReader br=new BufferedReader(isr); result=br.readLine(); System.out.println(result);
时间: 2024-11-04 16:09:21