【问题标题】:Sending data from java to php file将数据从java发送到php文件
【发布时间】:2015-05-16 18:32:23
【问题描述】:

我想通过 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();
     }

【问题讨论】:

标签: java php android


【解决方案1】:

尝试改变

 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);

【讨论】:

  • 没问题,我总是看到其他人在这里讨论复杂的事情,我主要做 PHP 工作,所以我选择最简单的方法,而不是最难编码的方法。很高兴它对你有用..
  • 我还有一个问题。在我的 php 文件中,我正在运行此查询。 $result = mysql_query("SELECT Company,Shares,Date FROM Portfolio WHERE Username='edward") or die(mysql_error());现在我想传递一个变量而不是爱德华,并在用户名将是该变量值时检索行。
  • 你应该使用mysqli而不是mysql,它是最新的形式。但这可能会帮助您入门。 $USER_NAME = $_REQUEST['用户名']; $Q = mysqli_query($conn, "SELECT * FROM portfolio WHERE Username = '$USER_NAME'"); if(mysqli_num_rows($Q) > 0) { //echo "用户名已经存在"; } else { do code } 这个框很难显示所有正确的编码,所以如果你看不懂,发布一个新问题的链接,我可以把它放在那里。
【解决方案2】:

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

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

$_POST['Username']

【讨论】:

  • 我尝试使用该方法,但通过该方法我得到了很多错误,所以我改用了这种方法。有没有办法使用这种方法发送可变数据?
  • 您的问题过于复杂。尝试在stackoverflow.com/questions/3324717/…中运行Java HTTP POST 请求代码
【解决方案3】:

在代码的基础上使其完整,即从 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;
    }
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多