0

I am trying to access a php file located on a web server using AsyncTask and the Default Http Client.

I keep getting this error:

Unable to resolve host "hostname": No address associated with hostname

This is my Activity where I try to access it:

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<String, Void, Boolean>{

    @Override
    protected Boolean doInBackground(String... params) {

        //location of file to retrieve
        String url = "http://hostname/12345/db_connect.php";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


        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();
    }

}

}

I've also added the following to my manifest file:

 <uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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!

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dbconnectiontest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1
  • add here your full manifest file Commented Jan 16, 2013 at 18:17

1 Answer 1

1

Check this solution

Hope it will solve your problem.

add this line to your AndroidManifest.xml file, just after the <manifest> tag and before the <application> tag:

 <uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

EDIT :

if it not working then the most common cause of this UnknownHostException error message. Another possible cause is that your internet connection is actually down, and you can test that with the "ping" command.

Sign up to request clarification or add additional context in comments.

2 Comments

I pinged the host and It sent the 4 packets successfully. I wonder is it something to do with my Campus security, It's a web server located on the campus you see
I just changed the url to www.google.com and no error, connection was established successfully. I will have to speak to IT department tomorrow to sort out the problem. Thanks for the help Dixit!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.