title: Test if a url exist in a fragment
body: <p>I am a newbie in android and speak a 'poor' english .</p>
  
  <p>I have a fragment where i have to test  <strong>if a url exist</strong>   before to execute a webview.</p>
  
  <p>In my first test i got a "<em>android.os.NetworkOnMainThreadException</em>"</p>
  
  <p>So , i use a <strong>AsyncTask</strong></p>
  
  <p>How can test in my fragment activity if the url exist ??</p>
  
  <p>I test under Android Studio and in debug  'doInBackground' is not executed ???</p>
  
  <p>In advance ,Many thanks for your support</p>
  
  <hr>
  
  <p><strong>here is my class AsyncTask:</strong></p>
  
  <pre><code>public class ExistUrl extends AsyncTask&lt;String, Void, Boolean&gt; {
  
     private static final String TAG = "CheckURL";
     String filename="";
  
     public ExistUrl(String URL) {
      this.filename=URL;
      Log.d(TAG, "url="+ this.filename);
    }
  
   @Override
      protected Boolean doInBackground(String... params) {
        //  validateParams(params);
          Log.v("name of file",this.filename);
          Boolean OK =MyUtilities.exists(params[0]);
          Log.v("***********  retour     exists class:", Boolean.toString(OK));
  
      return OK;
      }
  
     @Override
     protected void onCancelled() {
      super.onCancelled();
      Log.i(TAG, "Background cancelled.");
     }
  
     @Override
     protected void onPostExecute(Boolean result){
      super.onPostExecute(result);
         String message = null;
         if (result)
             message = "Url  succeeded.";
         else
             message = "Url failed.";
         Log.i(TAG, message);                               }
     }}
  </code></pre>
  
  <p><strong>Here is Myutilities.exists</strong></p>
  
  <pre><code>    public static boolean exists(String URLName){
  
      try {
          HttpURLConnection.setFollowRedirects(false);
          // note : you may also need
          //        HttpURLConnection.setInstanceFollowRedirects(false)
          HttpURLConnection con =
                  (HttpURLConnection) new URL(URLName).openConnection();
          con.setRequestMethod("HEAD");
          return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
      }
      catch (Exception e) {
          e.printStackTrace();
          return false;
      }
    }
  </code></pre>
  
  <p><strong>My fragment activity</strong></p>
  
  <pre><code>    public class HainautFragment extends Fragment {
  
      public HainautFragment(){}
  
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
                             View rootView = inflater.inflate(R.layout.fragment_hainaut, container, false);
      String Zone = "http://www.tournoi.org/tournoilive/affiche_categorie_dbf.asp?nuclb=1005&amp;datedeb=2013/9/13&amp;datefin=2013/9/22&amp;IdTournoi=309569";
  
                          ConnectionDetector cd = new ConnectionDetector(getActivity());
                          Boolean isInternetPresent = cd.isConnectingToInternet();
                          if (isInternetPresent) {
  
                                  new ExistUrl(Zone).execute();
  
          *********************//how to test return***************************
  
                                                         }
      return rootView;
  }
  </code></pre>