title: Accessing web server using HTTP Android
body: <p>I am trying to access a php file located on a web server using AsyncTask and the Default Http Client.</p>
  
  <p>I keep getting this error: </p>
  
  <p><strong>Unable to resolve host "hostname": No address associated with hostname</strong></p>
  
  <p>This is my Activity where I try to access it:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  Button b1;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      b1 = (Button)findViewById(R.id.button1);
      b1.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              new HTTPTask().execute();
          }
      });
  }
  
  private class HTTPTask extends AsyncTask&lt;String, Void, Boolean&gt;{
  
      @Override
      protected Boolean doInBackground(String... params) {
  
          //location of file to retrieve
          String url = "http://hostname/12345/db_connect.php";
          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
  
  
          try{
              HttpClient httpclient = new DefaultHttpClient();
  
              HttpPost httppost = new HttpPost(url);
  
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
              httpclient.execute(httppost);
              return true;
          }catch(Exception e){
              Log.e("log_tag", "Error in Connection " + e.toString());
              return false;
          }
  
      }
  
      @Override
      protected void onPostExecute(Boolean result) {
          super.onPostExecute(result);
          if(result){
              Toast.makeText(getApplicationContext(), "Connection Established!", Toast.LENGTH_SHORT).show();
          }else{
              Toast.makeText(getApplicationContext(), "Connection Failed!", Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          Toast.makeText(getApplicationContext(), "Connection initiating...", Toast.LENGTH_SHORT).show();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I've also added the following to my manifest file:</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.INTERNET" /&gt; 
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>I just can't think what it could be? I am working off a machine in my college, I am guessing it might be the security in the building? Any help is much appreciated!</p>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.dbconnectiontest"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" /&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt; 
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>