1
public class MainActivity extends Activity {

private ArrayList<NameValuePair> nameValuePairs;
private DefaultHttpClient httpclient;
private HttpPost httppost;
private HttpResponse response;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                InputStream is = null;
                // http post
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("post",
                        "20"));
                nameValuePairs
                        .add(new BasicNameValuePair("price", "14:30"));
                nameValuePairs.add(new BasicNameValuePair("description",
                        "19"));

                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            "http://mydomain111.site11.com/mysql_con.php");
                    httppost.setEntity(new UrlEncodedFormEntity(
                            nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                } catch (Exception e) {
                    Log.e("log_tag",
                            "Error in http connection" + e.toString());
                }
            } catch (Exception e) {

            }
        }

    });
    thread.start();

    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
   }
 }

And here's the php part:

<?php

      $con=mysql_connect("mysqlxxx.000webhost.com","xxxxx", "xxxxxxxx") or  die('Could not     connect: ' . mysql_error());
      mysql_select_db("xxxxxx_test",$con);
      if(isset($_POST['post'])&&isset($_POST['price'])&&isset($_POST['description'])){
           $post=$_POST['post'];
           $issue=$_POST['price'];
           $description=$_POST['description'];

      $sql="INSERT INTO test (post,price,description) VALUES ('$post','$issue','$description')";

      $r=mysql_query($sql);

      if(!$r)
           echo "Error in query: ".mysql_error();
       }
      else echo "no data endered";
      mysql_close();
?>

I tried this code and created a table on 000webhost and uploaded my php file there.But whenever I run the application from my android phone or emulator,on both case,the table is not updating with the values I want to insert,and also I opened the php file with my browser which I saved to htdocs on xampp and browser shows that:

" mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\Echo\mysql_con.php on line 5"

Now,how can I get this working,specially into my android phone.I am new to android and this code is for just learning purpose.

Edit: I haven't posted the log because it is clear and no error is showing

user3680660
  • 325
  • 1
  • 3
  • 6
  • You are trying to connect to 000webhost database externally? – h_s May 29 '14 at 11:57
  • I am trying to connect from my android cell;so externally,I guess. – user3680660 May 29 '14 at 11:59
  • You are a premium user? 000webhost database can't be connected externally by free users. – h_s May 29 '14 at 12:01
  • no,I am a free user.I didn't know that.I think that's the case then.But it's not working on emulator either actually. – user3680660 May 29 '14 at 12:05
  • Your code is widely open to SQL injection attacks, you might want to read up on that. Also, please be aware that the mysql extension (supplying the mysql_ functions) has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 29 '14 at 12:15

1 Answers1

0

Is you localhost running? check if yes then run the same page on your localhost as you are trying to inserting the data from app and print the same

Nik
  • 1