java - Getting UnknownHostException instead of SocketTimeOutExcepetion -
i new android , java. , trying learn android app development udacity. trying run code , expecting sockettimeoutexcepetion getting unknownhostexception.
try { final string base_url = "http://api.openweathermap.org/data/2.5/forecast/daily?"; final string zip = "zip"; final string mode = "mode"; final string units = "units"; final string count = "cnt"; final string app_id = "appid"; uri builturi = uri.parse(base_url).buildupon() .appendqueryparameter(zip, params[0] + ",in") .appendqueryparameter(mode,format) .appendqueryparameter(units, units) .appendqueryparameter(count, integer.tostring(numdays)) .appendqueryparameter(app_id, buildconfig.open_weather_map_api_key) .build(); string str = java.net.urldecoder.decode(builturi.tostring()); url url = new url(str); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setconnecttimeout(5000); urlconnection.setreadtimeout(5000); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { return null; } reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) buffer.append(line + "/n"); if (buffer.length() == 0) return null; forecastjsonstr = buffer.tostring(); log.v(log_tag,"json forcast string:" +forecastjsonstr); }catch(sockettimeoutexception e) { startactivity(new intent(getactivity(),checknet.class)); } catch (ioexception e) { log.e("fetchweathertask", "error:" + e.tostring()); return null; } i tested on phone running android version 4.0.4. , while testing had mobile data , wifi off
when mobile data , wifi turned off, socket layer unable resolve internet addresses (e.g. "openweathermap.org") ip address. why unknownhostexception.
whereas, when you're on network, , it's able resolve ip addresses, , server fails reply, sockettimeoutexception.
Comments
Post a Comment