Problems with special characters in URLs when accessing the network in Android

Source: Internet
Author: User

Component Example value Also known as
protocol http scheme
Authority Usernam e:[email protected]:8080  
User Info use Rname:password  
host host  
Port 8080  
file /directory/file?query  
Path /td> /directory/file  
Query quer Y  
ref ref fragment

A complete URL link can be represented like this:

Http://username:[email Protected]:8080/directory/file?query#ref:

(IPv6 URL format is to use the text address with the symbol "[" and "]" to close, simple example: http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80/index.html)

And in Android if you use HttpGet or HttpPost to access the network, there are two constructors in addition to a parameterless constructor, such as

1  PublicHttpGet (FinalURI Uri) {2         Super();3 Seturi (URI);4     }5 6     /**7      * @throwsIllegalArgumentException If the URI is invalid.8      */9  PublicHttpGet (FinalString URI) {Ten         Super(); One Seturi (Uri.create (URI)); A}

Note the 7th line, which tells us that if the string contains special characters, we need to convert these special characters.

Let's take a look at the Uri.create (URI) function:

1      Public StaticURI Create (String uri) {2         Try {3             return Newuri (URI);4}Catch(URISyntaxException e) {5             Throw Newillegalargumentexception (E.getmessage ());6         }7}

The two classes Url.class and Uri.class are described in the Android documentation as follows:

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

Url.class constructor:

public URL (string spec);p ublic URL (url context, string spec);p ublic URL (url context, string spec, URLStreamHandler handler );p the Ublic URL (string protocol, string host, string file);p the Ublic URL (string protocol, string host, int port, string file);p u Blic URL (String protocol, string host, int port, string File,urlstreamhandler handler);

Uri.class constructor:

 Public URI (String spec)  Public URI (string scheme, string Schemespecificpart, string fragment)  Public int port, String path, string query,string fragment)  Public URI (string scheme, string host, String path, string fragment)

In fact, these two classes construct a URI (or URL) from a string, mainly using the Libcore.net.url.UrlUtils class to identify string "//" "" ":" "&" "#" "@" These specific characters are then distinguished by a URL for each part of the.

Obviously, in addition to these non-letter special characters, such as spaces, "+" These special characters it is not processed. This requires that we replace these characters ourselves, either by using the Encode function or by replacing it directly with% (the ASCII code for that character).

1     Privatestring Encodeurl (string urlstr)2     {3URL =urlstr.contains ("%")? Urlstr.replace ("%", "%25"): urlstr;4URL =urlstr.contains ("+")? Urlstr.replace ("+", "%2b"): urlstr;5URL =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;Ten     //URL =urlstr.contains ("=")? Urlstr.replace ("=", "%3d"): Urlstr; One         returnURL; A}

The annotated part is because these characters I test in the general link can be successfully processed, but%,+ and spaces are needed to convert.

Finally, I chose this approach.

A beginner's inquiry.

Problems with special characters in URLs when accessing the network in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.