Http post - UnknownHostException: Android Java

I’ve been all over the internet trying to solve this and it’s busy driving me crazy, so… if anyone could point me in a direction, I would be grateful.

I’m trying to execute an HttpPost request to my web server, which continuously returns an UnknownHost exception. I’ve followed every article on the topic, and everything that’s been said I need to do, is done, but I still get the same result.

I’ve reverted to executing a post request on google, with the same result. Code below.

DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost(“”, 80, “http”);
//HttpGet httpget = new HttpGet(“about-us.html”);

HttpPost thePost = new HttpPost(targetHost.toURI());

BasicHttpContext localContext = new BasicHttpContext();
HttpResponse theResponse = null;

try { theResponse = client.execute(targetHost, thePost, localContext);
} catch (UnknownHostException e) {
    Log.i("postData", "Unknown Host Exception:\n\n" + e.toString());
} catch(Exception e){
    // Enter error-handling code here.

Previously my code was like this:

DefaultHttpClient httpclient = new DefaultHttpClient();
        String script_url = "";
        URI scriptUri = null;
        try {
            scriptUri = new URI("http","//","/users/login_mobile/");
        } catch (URISyntaxException e) {

        setResponse("Connecting to timetoreply...");
        if (scriptUri != null) {
            HttpPost httppost = new HttpPost(scriptUri);
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
            } catch (UnknownHostException e) {
                Log.i("postData", "Unknown Host Exception:\n\n" + e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.i("postData", "Exception:\n\n" + e.toString());

Again, producing the same exception.

My Android Application Manifest contains this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >

        android:theme="@style/AppTheme" >
            android:theme="@style/FullscreenTheme" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            android:label="@string/title_activity_splash" >

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

I’ve tried multiple SDKs and APIs, without any luck.

Anyone with some info on how I am supposed to execute this request, other than what can be found online as reference (since none of it works), would be helping me a truck load.


Hi. Usually that occurs if the device does not have an internet connection or if you not asking for INTERNET permission. On a side note, the HttpClient is not very efficient in android. The better option would be to use OkHttp or HttpUrlConnection. I would highly recommend using Square’s retrofit library, because it is very easy to use and is very customizable.

That being said, I have had success using HttpClient with the following code:

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(mContext, "urlToPostTo");

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookingId",String.valueOf(bookingID)));

httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse response = httpClient.execute(httpPost);

Hi, thank you for your response. Just on the Internet permissions and access, I test the process through both WiFi and Mobile Data, so I am definitely connected, as all my other apps receives data as expected. It’s just my app that doesn’t seem to connect. The permission directive is set in my manifest file, which is why none of this makes much sense.

Anyway, I will try the Retrofit library as well as your implementation described above and see what comes up.

Thanks again. :slight_smile: