将数据从java发送到php文件

时间:2015-05-16 18:32:24

标签: java php android

我想通过php文件在数据库中发布这些数据。现在,用户名,公司,股票和日期都发布在数据库中。我想发布" Edward"," AHCL"," 10"和" 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();
     }

3 个答案:

答案 0 :(得分:3)

尝试并更改

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

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

答案 1 :(得分:0)

您应该考虑向PHP页面发送GET或POST请求。 在此处查看如何发送POST请求:Sending HTTP POST Request In Java

然后,您的PHP脚本可以使用以下方法获取变量的值:

$_POST['Username']

答案 2 :(得分:0)

建立在代码上以使其完整,例如,将数据从JAVA传递(发布)到PHP,在两种情况下都进行获取和显示。 JAVA方面

      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;
    }
}
?>