Android 编码问题

来源:互联网 发布:怎么才能加入淘宝 编辑:程序博客网 时间:2024/06/03 21:51
php文件存在h盘 服务器中,由于记事本默认编码ascII 导致android端使用 ,UrlHttpConnection类的 GET方法后, 获取反馈(echo‘测试文本:登陆成功+123abc’)文本数据时“


@Overridepublic void run() {
{ Looper.prepare(); super.run(); try { URL url = new URL("http://192.168.1.109/mytest/index.php?name=" + user + "&psw=" + passwd); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream()); BufferedReader br = new BufferedReader(isr); String readbr; while ((readbr = br.readLine()) != null) line += readbr + "\n";//下面的操作都多此一举 // byte[] byteline = org.apache.commons.codec.binary.Base64.decodeBase64(line.getBytes()); //// Base64.decode(line,1) //// line = new String(byteline,"utf8"); //二进制转到字符称为编码,由字符到二进制称为解码 //str.getBytes("charset");//指定charset,即用底层存储的Unicode码来解析 charset这种编码格式的字节数组方式 //str.getBytes(); 如果括号中不写charset,则采用的是Sytem.getProperty("file.encoding"),即当前文件的编码方式, //Base64.decode(line.getBytes("utf8")); urlConnection.disconnect();//关闭http连接 } catch (UnknownHostException e) { e.printStackTrace(); line += e.toString(); } catch (IOException e) { e.printStackTrace(); line += e.toString(); } finally { Log.i("Mactivity_Thread", "line = " + line); Message msg = new Message(); // msg.set msg.obj = new String(line); mHandler.sendMessage(msg); Looper.loop(); }}
Android activity调用handler获取 msg.obj.toString() 时,所得值显示为乱码;多次转换编码无果,后改变 记事本的编码,成功!

0 0