2

hey guys i wanna ask a question about sending data to database to server using android app i am write script code by php like this .

     <?php 
            $coonect=mysql_connect("myhost of the site ","root","");
         if(!$coonect){
       echo "Data_base error";
         die(mysql_error());
         }
       ?>
       <?php
          $username=$_POST['menu_name'];
               $id=$_POST['id'];

         $db_select=mysql_select_db("TableName");
       if(!$db_select){
            die(mysql_error());
            echo" error";

     }
       $query= "INSERT INTO `DatabaseName`.`TableName` (
                   `name` ,
                    `id`
                        )
               VALUES (
                 '{$username}', '{$id}'
                        ); " ;

                  if($medo=mysql_query($query)){
             header("localhost/filename");
           exit;
               }else{
     echo"<p> Error</p>";
     die(mysql_error());
            }

               ?>

        <?php 
             mysql_close($coonect);
        ?>

and the code in the action buuton in the android like this .

           public void postData() {
// Create a new HttpClient and Post Header
   HttpClient httpclient = new DefaultHttpClient();
   HttpPost httppost = new HttpPost("http://mysite/script.php");

  try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("menu_name", "any data"));
    nameValuePairs.add(new BasicNameValuePair("id", "0345644"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

   } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
   } catch (IOException e) {
    // TODO Auto-generated catch block
  }
     } 

i dont change any thing in Androidmainfest file to establish the connection do i have to and if i should put something what isit ?!! but the apk work fine but it dosent work at all do i making something wrong in my code or i should put any permission in the Androidmainfest i dont know what's wrong ??? any Help pleas

2 Answers 2

2

As far as I know, any network communication would require this

<uses-permission android:name="android.permission.INTERNET" />

in the manifest file. But I can't really tell if it is due to this prevent you from doing your thing. Maybe after doing adding this, you could do a ping test from your android device using terminal emulator or apps like that. Let me know if you need help getting these information.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks makn i will try it and tell u if every thing is alright :)
1

Yes you need to give the INTERNET permission like below in the Manifest;

<uses-permission android:name="android.permission.INTERNET"/>

If you are trying to run the Android program on API level higher than 13, you have to code the connecting to Internet part in a background thread not in the main thread.

If you are running it below API level 13 you no need to worry about where to code the connecting part.

PS. : This is an example for handling HTTP POST and HTTP GET in Android

1 Comment

Thanks Man i will try it every thing seem to be good :) i hope it will work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.