java


Opening url with æøå, using URL.openStream (Java)


I am trying get some xml from the openweatherapi, but I have a problem when the url contains æ, ø or å.
What fails is:
URL website = new URL(params[0]);
InputStream inputStream = website.openStream(); // here it throws an FileNotFoundException
InputSource input = new InputSource(inputStream);
The params[0] could be: http://api.openweathermap.org/data/2.5/weather?q=århus,&mode=xml&units=metric
The entire code is:
public class GetWeatherInfo extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... params) {
String weatherInfo = null;
try {
URL website = new URL(params[0]);
InputStream inputStream = website.openStream();
InputSource input = new InputSource(inputStream);
SAXParserFactory saxp = SAXParserFactory.newInstance();
SAXParser sp = saxp.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();
HandlingXMLStuff handler = new HandlingXMLStuff();
xmlReader.setContentHandler(handler);
xmlReader.parse(input);
weatherInfo = handler.info.dataToString();
} catch (Exception e) {
e.printStackTrace();
}
return weatherInfo;
}
You need to escape the special characters. Construct a URI object and then use the toASCIIString() method to escape the special characters. This can be done as follows
try {
String url = "http://api.openweathermap.org/data/2.5/weather?q=århus,&mode=xml&units=metric";
URI uri = new URI(url);
URL escapedUrl = new URL(uri.toASCIIString());
} catch (URISyntaxException e) {
// handle exception
} catch (MalformedURLException e) {
// handle exception
}
Now, this means, that in your example, you can do the following:
URL website = new URL(new URI(params[0]).toASCIIString());

Related Links

Java MyBatis Enum string value
Interesting unchecked cast warning
Efficiently drawing many textures in android with OpenGL ES 2.0
Self assign in constructor of java
Running arduino sketch of c at arduino ide using java
Android use recursive method to find and colorize found texts
Check for double letters with indexOf?
Adding custom error page for servlet [duplicate]
Table 'mydatabase.user' doesn't exist [duplicate]
Android; getting an “the application has stopped unexpectedly” during runtime
what's the right way to override Controller Methods behaviour in Roo 2 (RC1)?
How to get spinner data in autocompletetext view suggession and on clicking spinner items it should redirect to next spinner in new activity
Round time by seconds
Two versions of the same class (1 static and one non static)
Jackson won't find my module
Packge level Namespace JAXB

Categories

HOME
jdo
openmp
proxy
single-sign-on
homebrew
lodash
relayjs
infragistics
basic
gnupg
adobe-analytics
spring-xd
mapserver
correlation
carthage
contextmenu
cx-freeze
physics-engine
typo3-6.2.x
log4js-node
windows-server-2000
kendo-datasource
librato
tinymce-4
directx-10
bosh
create-table
hybridauth
picasso
catalog
main
bitbucket-pipelines
automake
swift3.0.2
gesture
mixture-model
withings
starteam
trim
dism
jvm-languages
android-mediaprojection
turbogears
galleria
osx-mavericks
filepicker
togetherjs
aurelia-validation
lowpass-filter
pg-dump
font-size
namecoin
flow-control
quartz-composer
phishing
mikroc
autorest
contact-list
fputcsv
fadeout
separator
pundit
browser-bugs
cakephp-3.1
sailfish-os
pick
android-imagebutton
html-helper
dml
comexception
hyprlinkr
mcts
terminal-services
page-layout
spring-io
dotnetnuke-5
tws
plasma
html-editor
smtp-auth
browser-detection
runas
errai
dotnethighcharts
custom-backend
spyware
infobox
castle-monorail
multi-tier
premature-optimization
commodore
signal-handling

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App