HttpURLConnection请求出现乱码解决方案

来源:互联网 发布:驾考模拟软件 编辑:程序博客网 时间:2024/05/17 00:56

之前一直用的Httpclient,请求的时候没出现过问题,因为android 6.0中网络请求删掉了httpclient,就准备试试HttpURLConnection

String str = "http://220.248.121.170:6687/temp/CPHS_CLI_192.168.1.68.xml";        try {            String date = "";            URL url = new URL(str);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setRequestMethod("GET");            connection.connect();            if (connection.getResponseCode()==200) {                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));                BufferedReader bufferReader = new BufferedReader(reader);                  String inputLine  = "";                  while((inputLine = bufferReader.readLine()) != null){                      date += inputLine + "\n";                  }                  System.out.println(date);            }        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }

请求结果是

这里写图片描述

,百度了半天,尝试了各种解决方案,最后改了一个地方

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-16LE"));

编码方式改为UTF-16LE

?<callList>    <callPerson>        <pId></pId>        <roomID>2_1</roomID>        <pName>214号张世昌</pName>        <fWav>请全科二214号 张世昌 到1诊室就诊</fWav>        <upTime>2015-08-26 15:25</upTime>        <reFreshTime>3</reFreshTime>    </callPerson></callList>

ok,没问题了

0 0
原创粉丝点击