简体   繁体   中英

Simplest way to communicate data between php and android

I am trying to build an android application based on hotel management. The app simply provides the list of deals to the user. The website stores the data regarding the user and deals in sql tables in database.

Is there any simple way to communicate data such as deal details and user details like user name, hotel name, etc. from website to android app and viceversa.

I know this is possible using json. But I am finding it difficult to implement.

it is very painless using https://code.google.com/p/google-gson/ on android and json_encode() in PHP (take also a look on blackpanthers response) and here a short excerpt how to bind that thing in java on a concrete sample:

first implement json_encode() in PHP and call in in a browser so you can see the result, say, you get something like:

{"user_name":"my name", "user_surname":"my_surname", "phones": { "mobile":"11111" }}

now, you've got a JSON entity containing a properties "user_name", "user_surname", and "phones". whereas "phones" is a nested entity, containing a property "mobile".

now you create a java-class per entity, so we need two, the one containing the "phones" and the other one containing all properties, including the entity "phones"

class Phones {
    // with this annotation you can bind to
    // properties from JSON named differently than
    // the property in this class
    @SerializedName("mobile")
    String thePhone;
}

class MyJson {
    String user_name;
    String user_surname;
    Phones phones;
}

well, thats it :) ah ok, the final part

    ...
    InputStream is = new URL("http://www.my.php.returning.json").openStream();
    InputStreamReader isr = new InputStreamReader();
    MyJson myJson = new Gson().fromJson(isr , MyJson.class);
    ... //close stream, handle exceptions, etc.

    // now you've got that all in the myJson object...

here you go!

You may find the following tutorial useful: http://www.helloandroid.com/tutorials/connecting-mysql-database

This tutorial shows how you can use the MySQL database and PHP to send JSON to an android application.

(This is assuming you are using a MySQL database, if not, it is still a useful tutorial).

You can also use GSON, Google's API and the following tutorial: http://www.techrepublic.com/blog/app-builder/use-gson-to-work-with-json-in-your-android-apps/1386

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM