Http post - UnknownHostException: Android Java

#1

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("google.com", 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);
    System.out.println(theResponse.getStatusLine());
} 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 = "http://dashboard-timetoreply.semanticrew.co.za/users/login_mobile";
        URI scriptUri = null;
        try {
            scriptUri = new URI("http","//dashboard-timetoreply.semanticrew.co.za","/users/login_mobile/");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        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));
//http://dashboard-timetoreply.semanticrew.co.za/users/login_mobile/
                // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                setResponse(response.toString());
            } catch (UnknownHostException e) {
                Log.i("postData", "Unknown Host Exception:\n\n" + e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                setResponse(e.getMessage());
                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="http://schemas.android.com/apk/res/android"
    package="za.co.underthetable.timetoreply" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/rabbit"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="za.co.underthetable.timetoreply.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="za.co.underthetable.timetoreply.SplashActivity"
            android:label="@string/title_activity_splash" >
        </activity>
    </application>

    <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" />
</manifest>

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.

Pip

#2

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);
#3

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. smile

closed #5