Anjli Chhatwani
Anjli Chhatwani

Reputation: 3

Sending data from java to php file

I want to post this data in database through php file. Right now, Username,Company , Shares and Date are posted in database. I want to post "Edward", "AHCL", "10" and "23-04-2015"

public void Insert(View view){

          String UserName="Edward";

         //String Company = company.getText().toString();
         // Shares = shares.getText().toString();
         // String Date = date.getText().toString();

          String Company = "AHCL";
          String Shares= "10";
          String Date= "23-04-2015";


      try {
            // open a connection to the site
            URL url = new URL("http://10.0.2.2:8080//test/example.php");
            URLConnection con = url.openConnection();
            // activate the output
            con.setDoOutput(true);
            PrintStream ps = new PrintStream(con.getOutputStream());
            // send your parameters to your site
            ps.print("&Username=Username");
            ps.print("&Company=Company");               
            ps.print("&Shares=Shares");
            ps.print("&Date=Date");

            // we have to get the input stream in order to actually send the request
            con.getInputStream();

            // close the print stream
            ps.close();
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }







         // creating new product in background thread
         //new CreatePortfolio().execute();
     }

Upvotes: 0

Views: 10771

Answers (3)

KIPSANG HENRY
KIPSANG HENRY

Reputation: 11

Building on the code to make it complete ie passing(post) data from JAVA to PHP ,fetching and displaying in both cases. JAVA side

      String UserName="Edward";
      String Company = "AHCL";
      String Shares= "10";
      String Date= "23-04-2015";

  try {
        // open a connection to the site
        URL url = new URL("http://10.0.2.2:8080//test/example.php");
        URLConnection con = url.openConnection();
        // activate the output
        con.setDoOutput(true);
        PrintStream ps = new PrintStream(con.getOutputStream());
        // send your parameters to your site
        ps.print("&Username=" + Username);
        ps.print("&Company=" + Company);               
        ps.print("&Shares=" + Shares);
        ps.print("&Date=" + Date);


        // we have to get the input stream in order to actually send the request
        con.getInputStream();
        // print out to confirm
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
        System.out.println(line);
        // close the print stream
        ps.close();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }







     // creating new product in background thread
     //new CreatePortfolio().execute();

PHP

<?php 
foreach ($_POST as $key => $value) {
    switch ($key) {
        case 'firstKey':
             $Username = $value;
            break;
        case 'secondKey':
            $Company = $value;
            break;
    case 'thirdKey':
              $Shares= $value;
            break;
        case 'fourthKey':
            $Date = $value; 
            
$file = fopen("data.txt","a"); 
$details=$Username."\t".$Company."\t".$Shares."\t".$Date;
fwrite($file,$details."\n");
fclose($file);
       break;
        default:
            break;
    }
}
?>

Upvotes: 0

Jayce
Jayce

Reputation: 852

Try and change

 ps.print("&Username=Username");
    ps.print("&Company=Company");               
    ps.print("&Shares=Shares");
    ps.print("&Date=Date");

to

 ps.print("&Username=" + Username);
    ps.print("&Company=" + Company);               
    ps.print("&Shares=" + Shares);
    ps.print("&Date=" + Date);

Upvotes: 3

WilliamSF
WilliamSF

Reputation: 284

You should consider sending a GET or POST request to your PHP page. Check out how to send a POST request here: Sending HTTP POST Request In Java

Your PHP script can then get the value of your variables in the following using:

$_POST['Username']

Upvotes: 0

Related Questions