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试试
  • 相关阅读:
    “非工作总结”之快门—我的镜头见过你
    书摘:日本式管理和依靠自己
    寒冬日,找阳光
    模式自由(Schemafree)和数据存储的非格式化趋势
    心体澄澈,意气和平
    思考些管理的事情
    含沙射影,业镜照胆
    临崖之马,上滩之舟—凡事一定要区别不同情况对待
    [转]HttpContext.Current.Cache 和 HttpRuntime.Cache
    句柄、引用、指针与对象(转)
  • 原文地址:https://www.cnblogs.com/lianxu61/p/4091371.html
Copyright © 2011-2022 走看看