zoukankan      html  css  js  c++  java
  • android向web提交数据,中文乱码

    ============问题描述============


    源码如下所示, 这时候“张三”这个字符到web已经是两个“??”,怎么破,查了不少方法,
    如URLDecoder.decode(“张三”, "utf-8"),或者"张三".getBytes()都不好用啊,求破
    public static String GetXml() throws Exception {
    URL postUrl = new URL(“http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=张三”);
    HttpURLConnection connection = (HttpURLConnection) postUrl
    .openConnection();
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(true);
    connection.setRequestProperty("Content-Type", "text/xml");
    connection.connect();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    connection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码
    String line = "";
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
    sb.append(line);
    }
    reader.close();
    connection.disconnect();
    return sb.toString();
    }

    ============解决方案1============


    那你的web那边是不是也是使用的utf-8编码呢

    ============解决方案2============


    
    StringWriter writer = new StringWriter();
    
    IOUtils.copy(conn.getInputStream(), writer,"UTF-8");

    你这个方法试试

    ============解决方案3============


    直接用浏览器提交你这个地址 http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=张三
    你就能在地址栏看到是编码成什么样子了,也好测试返回的正常与否

    ============解决方案4============


    把UTF-8改成GBK试试
  • 相关阅读:
    如何高效查看 Docker 日志
    linux:有效使用docker logs查看日志
    FFmpeg命令行工具学习(一):查看媒体文件头信息工具ffprobe
    性能调优
    【禅道】Windows本地安装禅道2.0.9
    Handle
    Operate the elements
    Web功能测试常用方法
    Drop down box selection(Select)
    Iframe
  • 原文地址:https://www.cnblogs.com/lianxu61/p/4041891.html
Copyright © 2011-2022 走看看