title: Android: Getting HTTP status codes using DefaultHttpClient
body: <p>I'm a newbie in android development. I'm trying to write a small app that prints the HTTP status code of the queried URL.</p>
  
  <p>For any valid URL query, I'm getting 200 (as expected). but when I query a non-existent URL I get 0 (instead of a HTTP error code). This is what I can see from logcat-</p>
  
  <blockquote>
    <p>System.err   java.net.UnknownHostException: Unable to resolve host
    "www.adsxgp.com": No address associated with hostname</p>
  </blockquote>
  
  <p>What am I missing here?</p>
  
  <p>This is my code-</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private static String logtag = "TwoButtonApp";//for use as the tag when logging 
      private TextView textView;  
       /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              textView = (TextView)findViewById(R.id.TextBox01);
  
          }
  
          private class CloudLookup extends AsyncTask&lt;String, Void, String&gt;{
                  @Override
                  protected String doInBackground(String... urls) {
                  // TODO Auto-generated method stub
  
                  int responseCode = 0;
                  for (String url : urls){
                      DefaultHttpClient client = new DefaultHttpClient();
                      HttpGet httpGet = new HttpGet(url);
  
  
                      try {
  
                          HttpResponse execute = client.execute(httpGet);
                          responseCode = execute.getStatusLine().getStatusCode();
  
                          /*
                          URL obj = new URL(urls[0]);
                          con = (HttpURLConnection)obj.openConnection();
                          con.setRequestMethod("GET");
                          String USER_AGENT= "Mozilla/5.0";
                          con.setRequestProperty("User-Agent", USER_AGENT);
  
                          responseCode = con.getResponseCode(); */
  
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                      return Integer.toString(responseCode);
  
                  }
  
  
              @Override
              protected void onPostExecute(String result){
                  Toast.makeText(MainActivity.this, "Yo! I was called!" +result, Toast.LENGTH_LONG).show();
              }
          }
  
  
  
          public void onClick(View view) {
              CloudLookup cl = new CloudLookup();
              cl.execute(new String[]{"http://www.adsxgp.com"});
              Log.d(logtag,"onClick() called");
  
          }
  
  
          @Override
       protected void onStart() {//activity is started and visible to the user
        Log.d(logtag,"onStart() called");
        super.onStart();  
       }
       @Override
       protected void onResume() {//activity was resumed and is visible again
        Log.d(logtag,"onResume() called");
        super.onResume();
  
       }
       @Override
       protected void onPause() { //device goes to sleep or another activity appears
        Log.d(logtag,"onPause() called");//another activity is currently running (or user has pressed Home)
        super.onPause();
  
       }
       @Override
       protected void onStop() { //the activity is not visible anymore
        Log.d(logtag,"onStop() called");
        super.onStop();
  
       }
       @Override
       protected void onDestroy() {//android has killed this activity
         Log.d(logtag,"onDestroy() called");
         super.onDestroy();
       }
  
  
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>