.PHP file successfully works by creating a db connection and inserting sql.
My only conclusion then it is somewhere in the .java activity
..and yes I have set INTERNET PERMISSIONS within manifest.
MY ERROR: 01-03 21:56:05.784: W/System.err(1398): android.os.NetworkOnMainThreadException
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.email);
try {
postInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void postInternetData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.example.com/app/insert_tag.php");
try{
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("name", "android"));
post.setEntity(new UrlEncodedFormEntity (formparams));
HttpResponse response = httpclient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
<?php
// CREATE the connection
$connection = mysql_connect('XXXXXXXXXXX.db.godaddy.com','XXXXXXXXXXX','XXXXXXXXXXX');
if(!$connection) {
echo 'failure to connect host';
}
$mysql_select_db = mysql_select_db('XXXXXXXXXXX', $connection);
if(!$mysql_select_db) {
echo 'failure to select db';
}
$name = $_POST['name'];
$qry = "INSERT INTO httptest(id, name) VALUES ('', '" . $name . "');";
$result = mysql_query($qry, $connection);
IF (!$result){
echo 'failure to query db';
} else {
echo $result;
}
?>
After hours of troubleshooting through stackoverflow examples and man files, I fold.
Is there anything that stands out to you?
Thanks.
As of Android 3.0 if you have any network operations (eg. connecting to your site) on the main (aka GUI) thread, the application throws
NetworkOnMainThreadException. Try testing in an emulator running 2.2 to see if it works (and there are no other bugs in your code). Otherwise to make it work on 3.x and 4.x, you’ll need to take a look at threading in Android. A tutorial can be found here