【移动应用开发技术】Android App端与PHP Web端的简单数据交互的示例分析_第1页
【移动应用开发技术】Android App端与PHP Web端的简单数据交互的示例分析_第2页
【移动应用开发技术】Android App端与PHP Web端的简单数据交互的示例分析_第3页
【移动应用开发技术】Android App端与PHP Web端的简单数据交互的示例分析_第4页
【移动应用开发技术】Android App端与PHP Web端的简单数据交互的示例分析_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】AndroidApp端与PHPWeb端的简单数据交互的示例分析

实现流程/upload/information/20200623/125/126060.png流程说明具体实现<?php

header('Content-Type:text/html;charset=utf-8');/*设置php编码为utf-8*/

/*

*

Following

code

will

list

all

the

items

*/

//

array

for

JSON

response

$response

=

array();

//

include

db

connect

class

require_once

__DIR__

.

'/db_connect.php';

//

connecting

to

db

$db

=

new

DB_CONNECT();

//

get

all

items

from

items

table

$result

=

mysql_query("SELECT

*FROM

items

WHERE

type='1'")

or

die(mysql_error());

//

check

for

empty

result

if

(mysql_num_rows($result)

>

0)

{

//

looping

through

all

results

//

items

node

$response["items"]

=

array();

while

($row

=

mysql_fetch_array($result))

{

//

temp

user

array

$items

=

array();

$items["what"]

=

$row["what"];

$items["when"]

=

$row["when"];

$items["where"]

=

$row["where"];

$items["detail"]

=

$row["detail"];

$items["posttime"]

=

$row["posttime"];

$resultcontcat

=

mysql_query("SELECT

*FROM

guests")

or

die(mysql_error());

while

($row1

=

mysql_fetch_array($resultcontcat))

{

if

($row1["id"]

==

$row["gid"]){

$items["contact"]

=

$row1["contact"];

}

}

//

push

single

items

into

final

response

array

array_push($response["items"],

$items);

}

//

success

$response["success"]

=

1;

//

echoing

JSON

response

echo

json_encode($response,JSON_UNESCAPED_UNICODE);

}

else

{

//

no

items

found

$response["success"]

=

0;

$response["message"]

=

"No

items

found";

//

echo

JSON

echo

json_encode($response,JSON_UNESCAPED_UNICODE);

}

?>/upload/information/20200623/125/126061.pngJSON

{

"items":

[

{

"what":

"手表",

"when":

"2017-10-21

00:00:00",

"where":

"北区宿舍楼#504",

"detail":

"白色的手表,XX品牌",

"posttime":

"2017-10-21

13:03:09",

"contact":

"138123456"

},

{

"what":

"手机",

"when":

"2017-10-04

00:00:00",

"where":

"北区商店#111",

"detail":

"iphone6s,土豪金",

"posttime":

"2017-10-21

13:03:46",

"contact":

"137123456"

},

{

"what":

"电脑",

"when":

"2017-10-21

14:39:54",

"where":

"图书馆#203",

"detail":

"联想品牌笔记本",

"posttime":

"2017-10-21

17:08:14",

"contact":

"5670001"

},

{

"what":

"细说PHP",

"when":

"2017-09-21

13:03:46",

"where":

"南馆#403",

"detail":

"黑色封面,第二版《细说PHP》",

"posttime":

"2017-10-21

17:36:53",

"contact":

"63513641"

}

],

"success":

1

}提交数据<?php

header('Content-Type:text/html;charset=utf-8');/*设置php编码为utf-8*/

/*

*

Following

code

will

create

a

new

product

row

*

All

product

details

are

read

from

HTTP

GET

Request

*/

//

array

for

JSON

response

$response

=

array();

//

check

for

required

fields

if

(isset($_GET['what'])

&&

isset($_GET['when'])

&&

isset($_GET['where'])

&&

isset($_GET['detail'])&&

isset($_GET['contact']))

{

$what

=

$_GET['what'];

$when

=

$_GET['when'];

$where

=

$_GET['where'];

$detail

=

$_GET['detail'];

$contact

=

$_GET['contact'];

//

include

db

connect

class

require_once

__DIR__

.

'/db_connect.php';

//

connecting

to

db

$db

=

new

DB_CONNECT();

//

mysql

inserting

a

new

row

$result2

=

mysql_query("INSERT

INTO

guests(contact)

VALUES('$contact')");

$gidresult

=

mysql_query("SELECT

id

FROM

`guests`

WHERE

contact='$contact'");

while

($row

=

mysql_fetch_array($gidresult))

{

$gid=$row['id'];

}

$result1

=

mysql_query("INSERT

INTO

items(`what`,

`when`,

`where`,

`type`

,`gid`,

`detail`)

VALUES('$what',

'$when',

'$where',

'1',

'$gid',

'$detail')");

//

check

if

row

inserted

or

not

if

($result1

&&

$result2)

{

//

successfully

inserted

into

database

$response["success"]

=

1;

$response["message"]

=

"Items

successfully

created.";

//

echoing

JSON

response

echo

json_encode($response,JSON_UNESCAPED_UNICODE);

}

else

{

//

failed

to

insert

row

$response["success"]

=

0;

$response["message"]

=

"Oops!

An

error

occurred.";

//

echoing

JSON

response

echo

json_encode($response,JSON_UNESCAPED_UNICODE);

}

}

else

{

//

required

field

is

missing

$response["success"]

=

0;

$response["message"]

=

"Required

field(s)

is

missing";

//

echoing

JSON

response

echo

json_encode($response,JSON_UNESCAPED_UNICODE);

}

?>/upload/information/20200623/125/126062.pngAndoridprivate

void

queryLosts()

{

losts.clear();

new

Thread(new

Runnable()

{

@Override

public

void

run()

{

//

TODO

Auto-generated

method

stub

OkHttpClient

okHttpClient

=

new

OkHttpClient();

String

url

=

"http://webSite/androidapi/get_all_lost_items.php";

Request

request

=

new

Request.Builder()

.url(url)

.build();

Call

call

=

okHttpClient.newCall(request);

try

{

Response

response

=

call.execute();

String

res

=

response.body().string();

if

(res

!=

null

&&

!res.trim().equals("")){

JSONObject

jsonObject

=

new

JSONObject(res);

if

(jsonObject.getInt("success")

==

1){

JSONArray

jsonArray

=

jsonObject.getJSONArray("items");

for

(int

i

=

jsonArray.length()

-

1;i

>=

0;i--){

JSONObject

item

=

jsonArray.getJSONObject(i);

String

what

=

null;

try

{

what

=

item.getString("what");

}catch

(Exception

e){

}

String

when

=

null;

try{

when

=

item.getString("when");

}catch

(Exception

e){

}

String

where

=

null;

try{

where

=

item.getString("where");

}catch

(Exception

e){

}

String

detail

=

null;

try

{

detail

=

item.getString("detail");

}catch

(Exception

e){

}

String

contact

=

null;

try

{

contact

=

item.getString("contact");

}catch

(Exception

e){

}

Lost

lost

=

new

Lost();

lost.setTitle(what);

String

des

=

"地点:"

+

(where

==

null?"":where)

+"

"+"时间:"

+

(when

==

null?"":when)+"\r"

+

"

"+"描述:"

+

(detail

==

null?"":detail);

lost.setDescribe(des);

lost.setPhone(contact

==

null?"":contact);

losts.add(lost);

}

}

}

}

catch

(Exception

e)

{

e.printStackTrace();

showErrorView(0);

}

if

(losts

==

null

||

losts.size()

==

0)

{

handler.sendEmptyMessage(1);

return;

}

if

(losts.size()

>

0){

handler.sendEmptyMessage(2);

}

}

}).start();提交数据public

Handler

handler

=

new

Handler(){

public

void

handleMessage(android.os.Message

msg)

{

switch(msg.what){

case

1:

Toast.makeText(this,"提交成功",Toast.LENGTH_LONG).show();

break;

case

2:

Toast.makeText(this,"提交失败",Toast.LE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论