0

so what I want to do is say I have a PHP file that does a check from $_GET, so basically something like this;

<?php
$test = $_GET['test'];
if ($test == "test") {
   return true;
}
else {
  return false;
}
?>

And that will be saved on my server as test.php, I want to send a request to test.php from my Java file, get the return and do a test on the return.

So something like this

Request(http://example.com/test.php?test=test)
if (Request Returned True) {
   Do
}
else {
   Do...
}

Any idea on how to do that? I'm still new to Java so excuse this please..

user6613235
  • 49
  • 1
  • 10

1 Answers1

1

In php you need to echo some JSON:

<?php
$test = $_GET['test'];
if ($test == "test") {
   echo json_encode(['result'=>true]);
}
else {
    echo json_encode(['result'=>false]);
}

PS. Don't use ?> closing tag at the end of php file.

In java you could get Apache HttpClient, IOCommons (for IOUtils.toString) and Org.JSON (to parse JSON):

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://example.com/test.php?test=test");

HttpResponse response = httpClient.execute(getRequest);


BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

JSONObject json = new JSONObject(IOUtils.toString(br));

json.getString("result"); //result

I haven't checked it, but hopefully, it should work.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76