I am attempting to write a simple game server for an Android game using php with a mysql database.
My idea is to simply have the ‘droid application simulate a simple http post request
e.g http://www.mysite.com/missile/new.php?lat=58123456,long=-2123456,state=1
My web server is intended to then store a new missile as being launched in an mysql database from those E6 co-ords and then I intend to write another php file to allow all clients to peridoically check for missiles in flight.
I’ve tried out various bits of code I’ve found but with no 100% success.
If I type http://www.mysite.com/missile/new.php?lat=58123456,long=-2123456,state=1 into a browser bar – I get back
Array ( [launchlat] => 56123456,launchlong=2700123,state=0 ) because I’m just doing a simple
print_r($_GET);
echo '<br />';
in my php file.
However with my current code
try {
String reply;
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.mysite.com/missile/new.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("launchlat", "56123456"));
params.add(new BasicNameValuePair("launchlong", "2700123"));
params.add(new BasicNameValuePair("state", "0"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
reply = EntityUtils.toString(resEntity);
Log.i("RESPONSE",reply);
}
} catch (Exception e) {
e.printStackTrace();
}
I’m getting an empty array in reply.
Can anyone suggest a tweak (or completely new code to do the post and receive the reply please?
regards
Simon
I don’t know anything about android dev, but it seems like you are using
HttpPostbut your php is expecting aGET.Change your php to look in
$_POSTinstead of$_GET.If you need both GET and POST use
$_REQUEST