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..

4
  • google for rest api Commented Jul 20, 2016 at 10:46
  • In php, you need to output something, such as json. In Java, you need to parse the result Commented Jul 20, 2016 at 10:46
  • 1
    @bub Okay, will do. Commented Jul 20, 2016 at 10:50
  • @Steve How do I send the request to the PHP file from Java though? And how do I parse the result it returns? I'm fine with returning Json Commented Jul 20, 2016 at 10:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.