首页 » PHP » php实现post请求 接收android客户端json数据 存储数据库 并返回js

php实现post请求 接收android客户端json数据 存储数据库 并返回js

2016-06-14 19:40:02阅读(2346)

php实现post请求 接收android客户端json数据 存储数据库 并返回json

大纲

实现php端存储数据库类
实现php端接收post请求
实现php端返回json数据

实现android 客户端 jsonbean
实现android 客户端 http请求线程类

Linux服务器检查mysql数据库 ,数据是否正常插入

php后台

-实现php端存储数据库类

<?php class VideoClear{ /** * 检验ID 并存储数据库 操作 * * $id 客户端传来的 id * **/ public static function checkId($id){ //链接数据库 $con = mysql_connect("localhost","root","new-password"); //选择数据库 mysql_select_db("my_db",$con); //检验Id是否存在 $result = mysql_query("select * from recordClear where recordId=".$id.""); $num = mysql_num_rows($result); //获取星期 $time2 = date("N",time()); if($time2==2){//每周一开始清理 if($num){//数据存在 则 对次数增加 mysql_query("update recordClear set recordCount=recordCount+1 where recordId=".$id);//对数据自增 }else{//数据不存在 则建立新数据 $sql = "INSERT INTO recordClear(recordId,recordCount,cleardate) VALUES(".$id.",'1',".$time2.")"; $query = mysql_query($sql); } return "off".$id.mysql_error(); }else{ return "on"; } mysql_close($con); } } ?>

-实现php端接收post请求

<?php $json = file_get_contents("php://input"); $data = json_decode($json, true); require_once('./Response1.php'); Response1::json($data['id']); ?>

-实现php端返回json数据

<?php class Response1{ /** * * * * $codelangdao 朗道版本信息 返回的提示码 * $codezirui 子瑞版本信息 返回的提示码 * $message 返回的提示信息 * $data 返回的信息 */ public static function json($id,$data){ require_once('./VideoClear.php'); $tag = VideoClear::checkId($id); $result = array( 'clear_tag'=>"off", 'whether_clear'=>$tag, ); echo json_encode($result,128); exit; } } ?> 接下来是android客户端

-实现android 客户端 jsonbean
直接从php代码复制参数 保证书写不出错

/** * Created by Administrator on 2016/6/14. * author 王浩 * qq 1520777821 * 转载请注明 */ public class VideoClearBean { public String clear_tag; public String whether_clear; }

-实现android 客户端 http请求线程类

/** * Created by Administrator on 2016/6/14. * author 王浩 * qq 1520777821 * 转载请注明 */ public class UpdataThread extends Thread implements Runnable { public UpdataThread(){ } @Override public void run() { super.run(); executeClear(); } public void executeClear() { InputStream inputStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL("http://191.101.237.106/test1.php"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); urlConnection.setRequestProperty("Accept", "application/json"); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); //配置 json Gson gson = new Gson(); RequestBean json = new RequestBean(); json.id = "15"; json.verName = "1.41"; String jsonString = gson.toJson(json); Log.e("TAA", "TEST: 发出去的数据" + jsonString); wr.writeBytes(jsonString); wr.flush(); wr.close(); // try to get response int statusCode = urlConnection.getResponseCode(); if (statusCode == 200) { inputStream = new BufferedInputStream(urlConnection.getInputStream()); String str = VerUtils.inputStream2String(inputStream); Log.e("TAA", "TEST:" + str); VideoClearBean verBean = gson.fromJson(str, VideoClearBean.class); Log.e("TAA", "TEST Bean::" + verBean.toString()); } } catch (Exception e) { } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (urlConnection != null) { urlConnection.disconnect(); } } } }

-Linux服务器检查mysql数据库 ,数据是否正常插入
我们用 id 15检测下 客户端运行代码

然后查询Linux查询mysql

Last login: Tue Jun 14 07:05:05 2016 from 115.195.22.120 [root@guhaotechnology ~]# mysql -uroot -pnew-password Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | my_db | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use my_db; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------+ | Tables_in_my_db | +-----------------+ | recordClear | +-----------------+ 1 row in set (0.00 sec) mysql> select * from recordClear; +----------+-------------+------------+ | recordId | recordCount | cleardate | +----------+-------------+------------+ | 1 | 4 | NULL | | 1 | 4 | NULL | | 5 | 656 | NULL | | 6 | 1 | 1465878458 | | 7 | 1 | 1465878564 | | 8 | 1 | 2 | | 9 | 1 | 2 | | 11 | 3 | 2 | | 15 | 1 | 2 | +----------+-------------+------------+ 9 rows in set (0.00 sec)

很明显我们成功了 15的数据已经插入进去了

假如测试时你的服务器爆出了这样的错误
重启服务器即可 这是mysql的bug

Warning: mysql_query(): Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/VideoClear.php on line 34 Warning: mysql_query(): A link to the server could not be established in /var/www/html/VideoClear.php on line 34

接下来再看下客户端的log

06-14 19:03:30.894 29719-29749/com.qq.e.union.demo E/TAA: TEST: 发出去的数据{"id":"15","verName":"1.41"} 06-14 19:03:31.065 29719-29749/com.qq.e.union.demo E/TAA: TEST:{"clear_tag":"off","whether_clear":"off15"} 06-14 19:03:31.071 29719-29749/com.qq.e.union.demo E/TAA: TEST Bean::com.qq.e.union.demo.VideoClearBean@baa7a3

**以上就是和大家分享的
转载请注明 浩宇国香 博主原创
博主联系QQ1520777821
有问题Q我就行 博客上的时间比较少**

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹