Please help.
I have no idea why it doesn't work
I think that it should be the way that I pass variable from Java to Php is not correct.
I use localhost for testing(not tomcat or apache) just database in phpadmin.
The respond that I receive always fail.
public class test extends JApplet { public test(){ JButton myButton = new JButton("sendData"); myButton.setFont(new Font("Sansserif", Font.PLAIN, 14)); myButton.setSize(15, 10); myButton.addActionListener(new button()); add(myButton); } private class button implements ActionListener { public void actionPerformed(ActionEvent e) { PostMsg(10,"hello"); } } public void PostMsg(int score, String n){ try { String parametersAsString; byte[] parameterAsBytes; parametersAsString = "&name=" + n + "&score=" + score; parameterAsBytes = parametersAsString.getBytes(); // send parameters to server URL url = new URL("http://localhost/addtest.php"); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setDoInput(true); ((HttpURLConnection) con).setRequestMethod("POST"); con.setRequestProperty("Content=length", String.valueOf(parameterAsBytes.length)); OutputStream oStream = new DataOutputStream(con.getOutputStream()); oStream.write(parameterAsBytes); oStream.flush(); //get respond BufferedReader iStream = new BufferedReader(new InputStreamReader(con.getInputStream())); String aLine = iStream.readLine(); while(aLine != null) { if(aLine.contains("success")) System.out.println("success"); if(aLine.equals("")) { System.out.println("fail"); break; } } iStream.close(); oStream.close(); } catch (Exception e) { System.out.println("ERROR"+e.getMessage()); } } }
PHP Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include("../connectionJAVA/connect.php");
$name = $_POST['name'];
$score =$_POST['score'];
//insert data
$sql = "insert into java
values(null,'$name','$score')";
mysql_query($sql) or die("error=$sql");
echo "sucess";
?>
</body>
</html>


LinkBack URL
About LinkBacks
Reply With Quote