• 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试试
  • 相关阅读:
    leetcode[145]Binary Tree Postorder Traversal
    leetcode[146]LRU Cache
    leetcode[147]Insertion Sort List
    leetcode[148]Sort List
    Intro to WebGL with Three.js
    Demo: Camera and Video Control with HTML5
    js ar
    Jingwei Huang
    Tinghui Zhou
    MODS: Fast and Robust Method for Two-View Matching
  • 原文地址:https://www.cnblogs.com/lianxu61/p/4041891.html
Copyright © 2020-2023  润新知