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

Can't save entity with composite key
Cassandra query on timestamp column returns no values with Java's Instant type
Apache HTTP Client and conditional setting of proxy
How to preserve class type when passing object between Struts actions?
ByteBuddy - Rebasing native methods
LibGDX: The relationship between Screen interface and Game class
Converting a Cloud Storage file into a BigQuery Table/Dataset to then later query
Howto make the withdraw stop when the balance below 25$
getDayOfWeek in ArrayList equals itself, but getYear does not [duplicate]
LWJGL glBufferSubData() is not setting entire array
Getting “This class does not support SAAJ 1.1” when calling SoapMessage.getSOAPBody().hasFault()
magick.MagickException: No image to get image format
Sparks enableHiveSupport
Spring boot UrlFilenameViewController not working
Android Studio & firebase issues
How to design a system which sends records and retries sending them again, if an acknowledgement is not receieved? [closed]

Categories

HOME
osgi
image
c#-4.0
fluentd
tizen
dictionary
kde
path-finding
relay
framework7
jsp-tags
callback
android-youtube-api
virtualization
iggrid
google-translate
kibana-4
vifm
windows-server-2012
facebook-page
reactcsstransitiongroup
code-review
jprofiler
jtextfield
web-sql
mmap
captiveportal
internet-explorer-9
fgetcsv
filezilla
fluentvalidation
maquette
binary-data
siesta-swift
http-get
lxd
dartium
automake
abstract-class
firebase-admin
android-mediaprojection
total-commander
import-from-excel
sencha-touch-2.3
taffy
hibernate-tools
jedis
jquery-validate
celery-task
ionicons
jlink
dotnetzip
tizen-native-app
integrity
git-diff
lowpass-filter
elgg
diagnostics
flutterwave
spring-mongodb
sonarlint-vs
pycaffe
grails-tomcat-plugin
bonobo
phishing
paypal-express
player
multiple-regression
master-slave
revolution-r
photobucket
javax.sound.midi
tcpserver
lib.web.mvc
sdhc
icu4j
metaclass
wordml
method-parameters
proj4js
bridge.net
device-orientation
lustre
jsonpickle
codeigniter-url
gui-test-framework
html-helper
googlemock
zend-route
openexr
mesa
marmalade
dealloc
farseer
.aspxauth
dir
google-cloud-save
qt-faststart
orchardcms-1.7
eventlistener
rdoc
first-responder
word-processor
getmessage
netbeans-6.9
work-stealing
cinema-4d
servicehost
recent-documents
winbugs14
celltable
jmock
cxxtest

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App