Java PHP posting using URLConnector, PHP file doesn't seem to receive parameters
Posted
by Emdiesse
on Stack Overflow
See other posts from Stack Overflow
or by Emdiesse
Published on 2010-04-03T14:00:04Z
Indexed on
2010/04/03
14:03 UTC
Read the original article
Hit count: 403
Hi there, I am trying to post some simple string data to my php script via a java application. My PHP script works fine when I enter the data myself using a web browser (newvector.php?x1=&y1=...)
However using my java application the php file does not seem to pick up these parameters, in fact I don't even know if they are sending at all because if I comment out on of the parameters in the java code when I am writing to dta it doesn't actually return, you must enter 6 parameters.
newvector.php
if(!isset($_GET['x1']) || !isset($_GET['y1']) || !isset($_GET['t1']) || !isset($_GET['x2']) || !isset($_GET['y2']) || !isset($_GET['t2'])){
die("You must include 6 parameters within the URL: x1, y1, t1, x2, y2, t2");
}
$x1 = $_GET['x1'];
$x2 = $_GET['x2'];
$y1 = $_GET['y1'];
$y2 = $_GET['y2'];
$t1 = $_GET['t1'];
$t2 = $_GET['t2'];
$insert = "
INSERT INTO vectors(
x1,
x2,
y1,
y2,
t1,
t2
)
VALUES (
'$x1',
'$x2',
'$y1',
'$y2',
'$t1',
'$t2'
)
";
if(!mysql_query($insert, $conn)){
die('Error: ' . mysql_error());
}
echo "Submitted Data x1=".$x1." y1=".$y1." t1=".$t1." x2=".$x2." y2=".$y2." t2=".$t2;
include 'db_disconnect.php';
?>
The java code
else if (action.equals("Play")) {
for (int i = 0; i < 5; i++) { // data.size()
String x1, y1, t1, x2, y2, t2 = "";
String date = "2010-04-03 ";
String ms = ".0";
x1 = data.elementAt(i)[1];
y1 = data.elementAt(i)[0];
t1 = date + data.elementAt(i)[2] + ms;
x2 = data.elementAt(i)[4];
y2 = data.elementAt(i)[3];
t2 = date + data.elementAt(i)[5] + ms;
try {
//Create Post String
String dta = URLEncoder.encode("x1", "UTF-8") + "=" + URLEncoder.encode(x1, "UTF-8");
dta += "&" + URLEncoder.encode("y1", "UTF-8") + "=" + URLEncoder.encode(y1, "UTF-8");
dta += "&" + URLEncoder.encode("t1", "UTF-8") + "=" + URLEncoder.encode(t1, "UTF-8");
dta += "&" + URLEncoder.encode("x2", "UTF-8") + "=" + URLEncoder.encode(x2, "UTF-8");
dta += "&" + URLEncoder.encode("y2", "UTF-8") + "=" + URLEncoder.encode(y2, "UTF-8");
dta += "&" + URLEncoder.encode("t2", "UTF-8") + "=" + URLEncoder.encode(t2, "UTF-8");
System.out.println(dta);
// Send Data To Page
URL url = new URL("http://localhost/newvector.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(dta);
wr.flush();
// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}
wr.close();
rd.close();
} catch (Exception exc) {
System.out.println("Hmmm!!! " + exc.getMessage());
}
}
© Stack Overflow or respective owner