UPDATE 3
Everything is working now. I used this and this, instead of what i was using for POST.
UPDATE 2
I've just noticed that if I place my updates on descending order, it seems better organized.
I edited my code in my OP (down below) added a few comments and replaced the inputstream reader with ReadHttpResponse(String url)
from here
. This made me separate my PHP script into update.php and get.php instead of updateandget.php.
It turns out that the get.php is working fine, in the limited sense that it is just querying data, (not posting any values). The update.php or the Java code handling the connection to that url is messing up somehow.
At least I've identified that I'm not Posting correctly. I also updated OP to show update.php.
OP
This is my code running the doInBackground() of an AsyncTask
try{
//HttpURLConnection<-url.openConnection()
URL url = new URL(address);
HttpURLConnection conn =(HttpURLConnection) url.openConnection(); //remember to close
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(false);
// Write data to Server
//exctract parameters
String username = (String)arg0[0];
String lastLong = (String)arg0[1];
String lastLat = (String)arg0[2];
//encode parameters
String data = URLEncoder.encode("username", "UTF-8")+ "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("lastLong", "UTF-8")+ "=" + URLEncoder.encode(lastLong, "UTF-8");
data += "&" + URLEncoder.encode("lastLat", "UTF-8")+ "=" + URLEncoder.encode(lastLat, "UTF-8");
//Build outputstream handler (socket?)
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
//write,flush,close
wr.writeBytes( data );
wr.flush();
wr.close();
// Read Server Response
//BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//StringBuilder sb = new StringBuilder();
//String line = null;
//String returnmessage="";
//while((line = reader.readLine()) != null)
//{
// returnmessage="There is something at least";
// sb.append(line);
// break;
//}
//reader.close();
conn.disconnect();
//return returnmessage;
return ReadHttpResponse("http://www.pixeldonut.com/testing/tutorial/get.php");
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
No problems reading, (or with ReadHttpResponse). Problems with everything else apparently.
update.php:
<?php //lastknownlocation updateandget
//open con
$con=mysqli_connect("details");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
//=========================================================statistics snippet
if(array_key_exists('REMOTE_ADDR', $_SERVER)){
$ip=$_SERVER['REMOTE_ADDR'];
$address=$_SERVER['SERVER_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
$ip2=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip2="_";
}
$query="INSERT INTO statistics (ip,ip2,address) VALUES (";
$query.="'$ip','$ip2','$address'";
$query.=")";//echo $query;
$db_return=mysql_query($query);
if(!$db_return){
//echo "Your entry was unsuccessfully logged.";
}else{
//echo "Your entry was successfully logged.";
}
}
//==============================================================================
//update-insert
$username = $_POST['username'];
$param1= $_POST['param1'];
$param2= $_POST['param2'];
$query = "UPDATE users SET";
$query .=" param1='$param1'";
$query .=",param2='$param2' ";
$query .="WHERE username='$username'";//echo $query;
$result = mysqli_query($con,$query);
if(!result){
//couldnt find user, aborting
die();
}
//close con
mysqli_close($con);
?>
Thank you for your continued support and enthusiasm, Ebichuhamster.