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