Simplest way to communicate data between php and android

后端 未结 2 2068
故里飘歌
故里飘歌 2020-12-20 08:20

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

相关标签:
2条回答
  • 2020-12-20 08:54

    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!

    0 讨论(0)
  • 2020-12-20 09:10

    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

    0 讨论(0)
提交回复
热议问题