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);