zoukankan      html  css  js  c++  java
  • Android中访问网络时url中带有特殊字符的问题

    ComponentExample valueAlso known as
    Protocol http scheme
    Authority username:password@host:8080  
    User Info username:password  
    Host host  
    Port 8080  
    File /directory/file?query  
    Path /directory/file  
    Query query  
    Ref ref fragment

    一个完整的url链接可以表示成这样:

    http://username:password@host:8080/directory/file?query#ref: 

    (IPv6的url格式就是把文本地址用符号“[”和“]”来封闭,简单例如:http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html)

    而在android里面如果使用httpGet或httpPost来访问网络,除了一个无参构造函数,它们还有两个构造函数,例如

     1 public HttpGet(final URI uri) {
     2         super();
     3         setURI(uri);
     4     }
     5 
     6     /**
     7      * @throws IllegalArgumentException if the uri is invalid. 
     8      */
     9 public HttpGet(final String uri) {
    10         super();
    11         setURI(URI.create(uri));
    12     }

    注意第7行这句话,它告诉我们,如果这个字符串包含有特殊字符,我们需要对这些特殊字符进行转化。

    我们看一下URI.create(uri)这个函数:

    1     public static URI create(String uri) {
    2         try {
    3             return new URI(uri);
    4         } catch (URISyntaxException e) {
    5             throw new IllegalArgumentException(e.getMessage());
    6         }
    7     }

    android文档中对这两个类URL.class和URI.class描述如下:

    A URL(URI) is composed of many parts. This class can both parse URL(URI) strings into parts and compose URL(URI) strings from parts. For example, consider the parts of this URL(URI)。

    URL.class的构造函数:

    public URL(String spec);
    
    public URL(URL context, String spec);
    
    public URL(URL context, String spec, URLStreamHandler handler);
    
    public URL(String protocol, String host, String file);
    
    public URL(String protocol, String host, int port, String file);
    
    public URL(String protocol, String host, int port, String file,URLStreamHandler handler); 

    URI.class的构造函数:

    public URI(String spec)
    
    public URI(String scheme, String schemeSpecificPart, String fragment)
    
    public URI(String scheme, String userInfo, String host, int port, String path, String query,String fragment)
    
    public URI(String scheme, String host, String path, String fragment)

    事实上,这两个类通过一个String构造一个uri(或url),主要是利用libcore.net.url.UrlUtils这个类来标识String中"//""?"":""&""#""@"这些特定字符然后区分一个url各个部分的。

    很显然,除了这些非字母的特殊字符,另外如空格,“+”这些特殊字符它并没有处理。这需要我们自己对这些字符进行替换,可以使用encode函数,也可以直接replace为%(该字符的ascii码)。

     1     private String encodeUrl(String urlStr)
     2     {
     3         url =urlStr.contains("%")?urlStr.replace("%", "%25"):urlStr;
     4         url =urlStr.contains("+")?urlStr.replace("+", "%2B"):urlStr;
     5         url =urlStr.contains(" ")?urlStr.replace(" ", "%20"):urlStr;
     6     //    url =urlStr.contains("/")?urlStr.replace("/", "%2F"):urlStr;
     7     //    url =urlStr.contains("?")?urlStr.replace("?", "%3F"):urlStr;
     8     //    url =urlStr.contains("#")?urlStr.replace("#", "%23"):urlStr;
     9     //    url =urlStr.contains("&")?urlStr.replace("&", "%26"):urlStr;
    10     //    url =urlStr.contains("=")?urlStr.replace("=", "%3D"):urlStr;
    11         return url;
    12     }

    被注释的部分是因为这些字符我测试中一般链接是可以成功被处理的,但是%,+和空格是需要转换的。

    最后我选择了这种处理方式。

    新手初探,求指正。

  • 相关阅读:
    context-annotation
    bean-annotation
    K-means算法
    基于概率的分类-贝叶斯分类
    Application
    ConfigurableApplicationContext
    相关性分析
    方差分析
    Java 大写金额转换成数字
    linux 遍历文件添加index
  • 原文地址:https://www.cnblogs.com/LNOMP/p/4282394.html
Copyright © 2011-2022 走看看