php - Values are not inserting on mysql database from android device

162
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

144

Answer

Solution:

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

People are also looking for solutions to the problem: javascript - Disable a button in page1 based on a textbox value from page2 (AJAX, PHP,jQuery)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.