title: Web request not 404 proof and crashes app
body: <p>I have an app where i read some data from online api every 30sec. I have somewhat secured my app so that it will check if the device is connected to network and data can be trasfered.</p>
  
  <pre><code>private boolean isNetworkAvailable() {
          ConnectivityManager connectivityManager
                  = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
          return activeNetworkInfo != null &amp;&amp; activeNetworkInfo.isConnected();
      }
  </code></pre>
  
  <p>But this does not include situations when i am connected to a network which needs authentification for internet access. Or when you have only local connectivity enabled.
  The app tries to get data from that api and it crashes. Here is the request code:</p>
  
  <pre><code> public static JSONObject requestWebService(String serviceUrl) {
      disableConnectionReuseIfNecessary();
  
      HttpURLConnection urlConnection = null;
      try {
          // create connection
          URL urlToRequest = new URL(serviceUrl);
          urlConnection = (HttpURLConnection)urlToRequest.openConnection();
          urlConnection.setConnectTimeout(10000);
          urlConnection.setReadTimeout(10000);
  
          // handle issues
          int statusCode = urlConnection.getResponseCode();
          if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
              // handle unauthorized (if service requires user login)
          } else if (statusCode != HttpURLConnection.HTTP_OK) {
              // handle any other errors, like 404, 500,..
          }
  
          // create JSON object from content
          InputStream in = new BufferedInputStream(
                  urlConnection.getInputStream());
          return new JSONObject(getResponseText(in));
  
      } catch (MalformedURLException e) {
          // URL is invalid
      } catch (SocketTimeoutException e) {
          // data retrieval or connection timed out
      } catch (IOException e) {
          // could not read response body
          // (could not create input stream)
      } catch (JSONException e) {
          // response body is no valid JSON string
      } finally {
          if (urlConnection != null) {
              urlConnection.disconnect();
          }
      }
  
      return null;
  }
  </code></pre>
  
  <p>All i need is that this app will not crash. How can i achieve this?</p>
  
  <p>SOLUTION: it needed null check before reading JSON object... it was in different method:</p>
  
  <pre><code>public void check(){
      JSONArray jsonArray = null;
      if(isNetworkAvailable()) {
          JSONObject dataZNetu = requestWebService("http://census.soe.com/get/ps2:v2/world_event?type=METAGAME");
          if(dataZNetu != null) {  //THIS WAS THE SOLUTION TO MY PROBLEM
              try {
                  jsonArray = dataZNetu.getJSONArray("world_event_list");
                  for (int i = jsonArray.length() - 1; i &gt;= 0; i--) {
                      JSONObject tempObj = jsonArray.getJSONObject(i);
                      int worldId = tempObj.getInt("world_id");
                      int stateID = tempObj.getInt("metagame_event_state");
                      int eventType = tempObj.getInt("metagame_event_id");
                      int instanceId = tempObj.getInt("instance_id");
                      checkUIchange(worldId, stateID, eventType, instanceId);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
  
      }
  }
  </code></pre>