zoukankan      html  css  js  c++  java
  • android网络

    ji那天在写一个下载的东西,下载的路径中出现了中文域名,然后就抛出了如下的异常

    03-10 22:59:56.370: W/System.err(22605): java.io.FileNotFoundException: http://*******/file/20140310/ht6p多线程编程.pdf
    03-10 22:59:56.385: W/System.err(22605): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    03-10 22:59:56.385: W/System.err(22605): at com.ericssonlabs.BarCodeTestActivity.Download(BarCodeTestActivity.java:179)
    03-10 22:59:56.415: W/System.err(22605): at com.ericssonlabs.BarCodeTestActivity$1$1.run(BarCodeTestActivity.java:68)

    自己对这个问题很好奇,然后,找了半天,终于明白在android中对中文需要强制转换成UTF-8的编码,不然就会抛出这个异常(在这里感谢一下java的异常处理,很给力,程序没有崩溃)

    核心代码如下:

    1 result = a + "/"+ URLEncoder.encode(temp[temp.length - 1], "utf-8");

    在这之中a是前面非中文的部分,后面转换的只是中文的部分,不然可能也会无效哦

    不过在这里,我还是遇到了另外一个问题

    转换之后的结果之后莫名多出了一个%0A,上网查了一下,是换行符号,原因我还没找到

    03-10 22:59:55.710: I/System.out(22605): http://******/file/20140310/ht6p多线程编程.pdf
    03-10 22:59:55.715: I/System.out(22605): ht6p%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%BC%96%E7%A8%8B.pdf%0A 
    http://upan.oureda.cn/file/20140310/ht6p%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%BC%96%E7%A8%8B.pdf
    03-10 22:59:55.725: I/System.out(22605): http://upan.oureda.cn/file/20140310/ht6p%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%BC%96%E7%A8%8B.pdf
    03-10 22:59:55.730: I/System.out(22605): ht6p多线程编程.pdf

    我面对这个情况,在几个不同的地址尝试发现都有这个字符之后,采用了比较无赖的处理办法,直接删掉后面三个字符

    整个函数的代码如下

     1 public static String toUtf8(String str) {
     2         System.out.println(str + "   :");
     3         String result = null;
     4         String[] temp = str.split("/");
     5         String[] temp_ = Arrays.copyOf(temp, temp.length - 1);
     6         String a = null;
     7         for (int i = 0; i < temp_.length; i++) {
     8             if (i == 0) {
     9                 a = temp_[i];
    10             } else {
    11                 a += "/";
    12                 a += temp_[i];
    13             }
    14         }
    15         try {
    16             System.out.println(URLEncoder
    17                     .encode(temp[temp.length - 1], "utf-8") + "   aq");
    18         } catch (UnsupportedEncodingException e1) {
    19             // TODO Auto-generated catch block
    20             e1.printStackTrace();
    21         }
    22         System.out.println(a + "   e");
    23         try {
    24             result = a + "/"
    25                     + URLEncoder.encode(temp[temp.length - 1], "utf-8");
    26         } catch (UnsupportedEncodingException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         }
    30         System.out.println(result.substring(0, result.length() - 3));
    31         return result.substring(0, result.length() - 3);//无赖的办法
    32 
    33     }

    这里面带着我调试时候的输出日志,原谅我的偷懒,这里面的来回转换有点混乱,有哪位大神有更好的方案,欢迎您提出来

  • 相关阅读:
    C# 多线程 弹出模态MessageBox的一种方法
    Selection.Delete Shift:=xlUp
    Selection.Delete Shift:=xlUp
    C# 源码
    C# 源码
    C# string格式的日期时间字符串转为DateTime类型的方法
    C# string格式的日期时间字符串转为DateTime类型的方法
    在Excel VBA中,单元格的.interior.color的值是什么格式的?
    在Excel VBA中,单元格的.interior.color的值是什么格式的?
    C# 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化
  • 原文地址:https://www.cnblogs.com/cwr941012/p/3592962.html
Copyright © 2011-2022 走看看