最简单的沟通PHP和Android之间的数据的方法最简单、方法、数据、Android

2023-09-07 23:56:33 作者:祢不坚强、懦弱给谁看゛

我试图建立一个基于酒店管理Android应用程序。该应用程序只是提供给用户的交易清单。本网站对于存储在SQL数据库表中的用户和交易数据。

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.

有没有简单的方法来传送数据,如交易细节和用户的详细信息,如用户名,酒店名称等从网站到Android应用程序,反之亦然。

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.

我知道这是可能使用JSON。但我发现很难实现。

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

推荐答案

这是很痛苦使用的 HTTPS://$c$c.google.com/p/google-gson/ 在Android和json_en code()在PHP(带也blackpanthers响应看看)和这里简短的摘录如何绑定在java中那个东西对一个具体的例子:

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:

首先实现json_en code()在PHP,并呼吁在浏览器中,所以你可以看到结果,比方说,你是这样的:

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

{用户名:我的名字,user_surname:my_surname,手机:{手机:11111}}

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

现在,你已经有了一个包含属性用户名,user_surname和手机的JSON实体。而手机是一个嵌套的实体,包含财产移动。

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

现在你为每个实体创建一个java类,所以我们需要两个,一个包含手机,另一个包含所有属性,包括实体手机

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;
}

好,那它:)啊OK,最后一部分

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!