解决httpurlconnection获取网页数据部分中文乱码问题

“`
public void doGet(final String urlStr) throws CommonException {
final StringBuffer sb = new StringBuffer();
new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                URL url = new URL(urlStr);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.setRequestProperty("Charset", "UTF-8");
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    int len = 0;
                    //原因就出在这里,直接我开的字节1024这回造成如果是一个中文字符正好在这个1024的临界点,这样就会出现中文乱码,所以我就直接将大小开到60000,哈哈哈。
                    byte[] buf = new byte[60000];
                    while ((len = is.read(buf)) != -1) {
                        sb.append(new String(buf, 0, len, "UTF-8"));
                    }
                    jsoup_jiexi(sb.toString());
                    is.close();
                } else {
                    throw new CommonException("访问网络失败00");
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                try {
                    throw new CommonException("访问网络失败11");
                } catch (CommonException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }).start();
}```
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 HttpURLConnection 获取网络数据可以支持 XML 格式返回和 JSON 格式返回。 对于 XML 格式返回,可以使用 Java 内置的 DOM 或 SAX 解析器来解析 XML 数据。下面是一个示例代码: ```java URL url = new URL("http://example.com/data.xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/xml"); InputStream stream = conn.getInputStream(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stream); // 对 XML 数据进行解析,例如: NodeList nodeList = doc.getElementsByTagName("item"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); String title = node.getAttributes().getNamedItem("title").getNodeValue(); // ... } ``` 对于 JSON 格式返回,可以使用第三方库,例如 Google 的 Gson、Apache 的 Jackson 等来解析 JSON 数据。下面是一个示例代码: ```java URL url = new URL("http://example.com/data.json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); InputStream stream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(response.toString(), JsonObject.class); // 对 JSON 数据进行解析,例如: JsonArray jsonArray = jsonObject.getAsJsonArray("items"); for (JsonElement element : jsonArray) { JsonObject item = element.getAsJsonObject(); String title = item.get("title").getAsString(); // ... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值