2

I am trying to communicate to my server using Android Application. I googled it and get to know about REST API. I followed the article http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/, and wrote backend code.

As far as I know, what I got is nothing but backend code, which manages various responses from the user. Earlier, what I doing was, coding manually like this:

public function add_data()
    {
        // Adding data to the table using this controller.

        if ( isset($_POST['submit']))
        {
            if ( isset($_POST['heading']) && isset($_POST['content']) && isset($_POST['references']))
            {
                // echo "I am in post";
                if ( !empty($_POST['heading'] && !empty($_POST['content'] && !empty($_POST['references']))))
                {
                    $this->load->model('Admin_model');
                    $this->Admin_model->insert_data($_POST['heading'],$_POST['content'],$_POST['references']);
                    // echo "Data is inserted successfully.";
                }
            }   
        }

        $this->admin();
    }

What above code does add data to the database.

Now, I want to know how the REST API coding is different from the backend coding in a framework ( Say code igniter, in case of PHP)? The backend coding gives output in json form.

What are some better ways to communicate with the server in android application?

Please help me.

1 Answer 1

2

Codeigniter + REST

Frameworks like codeigniter and Restfull "backends" are not mutually exclusive. You could in fact write a RESTful API that takes advantage of CI.

CI's typical UI structure is segmented as follows...

example.com/class/function/ID

There is no reason why you can't use this std structure to implement a RESTful API. Though using CI is probably overkill.

Example CI REST Implementation

(Check out the README on this site. Has some great examples plus a tutorial that might help you understand how to move forward)

As far as communicating with an Android application using a RESTful API is a great option since it means your application doesn't need to be aware of the servers underlying technology and therefore you gain some flexibility in how you manage your system/code.

Android + Sockets

Alternatively you could look into using other protocols such as Sockets. There are numerous options for implementing Sockets for different languages and frameworks.

If you take this approach I would suggest ditching PHP.

Android Docs

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

6 Comments

Thank you for the answer. "You could in fact write a RESTful API that takes advantage of CI." So, how can I do that? Do I need to use some extra code/concepts or simply taking $_POST / $_GET input/output and printing results in JSON will do?
If your focus is to do it correctly then I would strongly suggest you read up on what RESTful APIs are. A simple JSON return from POST/GET does not necessarily qualify. In my answer I linked to a Great CI REST class which can get you started.
Will ditching PHP work? Basically I want to communicate with server. I know PHP that's why I am talking about PHP here.
Understood. The ditching PHP part was meant for if you decided to implement Socket based communication. PHP in that specific case doesn't have the greatest support. Something like Node.js would be better
Does implementation of socket not require backend programming?
|

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.