Home Ask Login Register

Developers Planet

Your answer is one click away!

Saran Sankaran February 2016

Getting UnknownHostException instead of SocketTimeOutExcepetion

I am new to android and Java. And I am trying to learn android app development from UDACITY. I was trying to run this code and I am expecting a SocketTimeOutExcepetion but what I am getting is 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 it on my phone running Android version 4.0.4.

Answers


Dmitry Brant February 2016

When your mobile data and wifi are turned off, the socket layer is unable to resolve internet addresses (e.g. "openweathermap.org") into an IP address. This is why you get an UnknownHostException.

Whereas, when you're on a network, and it's able to resolve IP addresses, and the server fails to reply, you will get a SocketTimeoutException.


Pooya February 2016

If you want to simulate the exception do the following:

  1. Disconnect your data and connect your Wi-Fi
  2. Edit your setting of your Wi-Fi connection
  3. Change to static IP and put 169.254.0.50 for IP, 255.255.0.0 for subnet and 169.254.0.1 for gateway
  4. Change the BASE_URL = "192.241.169.168/data/2.5/forecast/daily?"
  5. Run your app

Post Status

Asked in February 2016
Viewed 1,721 times
Voted 6
Answered 2 times

Search




Leave an answer


Quote of the day: live life