android 特殊字符检查,Android中访问网络时url中带有特殊字符的问题

本文介绍了如何在Android中处理网络请求中的URL编码问题,特别是HttpGet构造函数中特殊字符的处理,以及URL类和URI类在构建和解析URL时的角色,重点讲解了`encodeUrl`函数的实现和常见字符转义规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ComponentExample valueAlso known as

http

scheme

username:password

host

8080

/directory/file?query

/directory/file

query

ref

fragment

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

http://username:[email protected]: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(finalURI uri) {2 super();3 setURI(uri);4 }5

6 /**

7 *@throwsIllegalArgumentException if the uri is invalid.8 */

9 public HttpGet(finalString uri) {10 super();11 setURI(URI.create(uri));12 }

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

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

1 public staticURI create(String uri) {2 try{3 return newURI(uri);4 } catch(URISyntaxException e) {5 throw newIllegalArgumentException(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的构造函数:

publicURI(String spec)publicURI(String scheme, String schemeSpecificPart, String fragment)public URI(String scheme, String userInfo, String host, intport, 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 privateString 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 returnurl;12 }

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

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

新手初探,求指正。

原文:http://www.cnblogs.com/LNOMP/p/4282394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值