Java.net.URISyntaxException Solution __javaee

Source: Internet
Author: User

Recently in the use of httpclient access to capture the exchange rate, in order to save effort, directly using

String url = "Http://api.liqwei.com/currency/?exchange=usd|cny&count=1"; HttpClient client = new Defaulthttpclient (); HttpGet httpget = new HttpGet (URL); HttpResponse response = Client.execute (HttpGet);
There was no problem with this method before, but this time I reported the following error:
Java.net.URISyntaxException:Illegal character in query at index 44

Looked up some online information, said the address of the special characters, such as ' | ' & ' and so on. Therefore, you cannot access it directly by using a string instead of a URI. Special characters must be replaced with%0xxx methods. But this approach is not intuitive. So you can only turn the string into a URL, and then the URL to generate a URI method to solve the problem. The code is as follows

URL URL = new url (strurl); Uri Uri = new uri (Url.getprotocol (), Url.gethost (), Url.getpath (), Url.getquery (),  null); HttpClient client = newdefaulthttpclient (); HttpGet httpget = new httpget (URI); 

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.