1

I have a PHP page (test.php) that has to respond to a HTTP GET request sent from an android application. The response has to be via a json object. How do I do that?

For reference :- Below is a php code that checks the login details from a login form. The http request would be the username and password sent from an android application that contains the login form. The response from this page would be the success or failure message.

<?php
        session_start();
         require('connect.php');
        if (isset($_GET['username']) and isset($_GET['password']))
        {
            $username = $_GET['username'];
            $password = $_GET['password'];
            $query = "SELECT * FROM `staff_reg` WHERE username='$username' and password='$password'";

            $result = mysql_query($query) or die(mysql_error());
            $count = mysql_num_rows($result);
            if ($count == 1)
            {
                $_SESSION['username'] = $username;
            }
            else
            {
                echo "Invalid Username or Password";
            }
        }
        if (!isset($_SESSION['username']))
        {
            echo "Session Expired";
            echo "<br><a href='staff_login.php'>Click here</a> to login again"; 
            echo "<br><a href='index.html'>Click here</a> to go back to the homepage"; 
            $username = $_SESSION['username'];
        }
        else{
            $username = $_SESSION['username'];
            echo "<p>Welcome ".$username."<br>";
            echo "This is the Staff Portal<br>";
            echo "<a href='logout.php'>Logout</a></p>"; 
    ?>

3 Answers 3

2

I think it may help you..

<?php
        session_start();
         require('connect.php');
        //create an variable $response
        //Use GET instead of POST since you are sending data in GET method
        if (isset($_GET['username']) and isset($_GET['password']))
        {
            $username = $_GET['username'];
            $password = $_GET['password'];
            $query = "SELECT * FROM `staff_reg` WHERE username='$username' and password='$password'";

            $result = mysql_query($query) or die(mysql_error());
            $count = mysql_num_rows($result);
            if ($count == 1)
            {
                $response="Success";
            }
            else
            {
                $response="Failure";
            }
        //now echo it as an json object
            echo json_encode($response);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

All the answers were helpful but this felt more "usable". Thank you all
1

You can send your response as json encoded data e.g. lets say your response data is

$user = array('firstName' =>'abc', 'lastName' =>'xyz'); 
$response = json_encode($user);

Comments

0

If this is an api for the android app you mentioned then you cannot use sessions in it. You have to find another way for authorizing the user.

You can encode your strings and arrays in json format but you'd better make an object and encode it. That way you can access the values by the name you specified in the api.

For example:

$obj->status = "error";

Or

$obj->status = "success";

And in the end:

echo json_encode($obj);

And you can access it in the application with this format:

   //String response = httpResponse
   JsonObject object = new JsonObject(response);
   String status = object.getString("status");
   if(status.equals("error")){
        //handle the error
   } else {
        //login was successful and you can show the user the proper activity
   }

As for sending the request to the api i recommend you to use the library AQuery.

The library can simplify your work with http requests. it works as follows:

    AQuery aq = new AQuery(context);
    aq.ajax(url, JsonObject.class, new AjaxCallback<JsonObject>(){
    @Override
    public void callback(String url, JSONObject object, AjaxStatus status) {
        //object is the json object sent from the server
    }
    });

I hope this helps you

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.