Home Backend Development PHP Tutorial android客户端跟php服务简单交互

android客户端跟php服务简单交互

Jun 13, 2016 pm 12:23 PM
executor quot

android客户端和php服务简单交互

android客户端和php+mysql+apache搭建之间的简单交互,实现log信息存储。

实现原理就是android客户端发送请求,传给服务器log信息,服务器收到这些,连接数据库进行存储,并将存储后的状态返回给客户端。

服务器端:

先在mysql里面建一个testlog的数据库,里面有一个log_info表,记录了LogCategory,System,Executor,Action等信息。

在php的虚拟目录下新建一个php项目testlog,创建conn.php和log_deal.php文件。


<?php include_once(&#39;conn.php&#39;);		//echo &#39;$_POST接收:</br>';	$System = $_POST['System'];	$LogCategory = $_POST['LogCategory'];	$Executor = $_POST['Executor'];	$Action = $_POST['Action'];	$sqlstr = "insert into log_info(System,LogCategory,Executor,Action,CreateTime) values('".$System."','".$LogCategory."','".$Executor."','".$Action."','".date('Y-m-d H:m:s')."')";    if (mysql_query($sqlstr)){    	echo "succeed";    } else {    	die(mysql_error());    	echo "error";    }?>
Copy after login
服务器搭建完成。

android客户端:

布局随意写一下就OK了

下面是主要代码:

class SendlogHandler implements Runnable{        @Override        public void run() {            try {                String url = "http://localhost/testlog/log_deal.php";                String result = null;                boolean isSendSucceed = false;                HttpPost httpRequest = new HttpPost(url);                List params = new ArrayList();                params.add(new BasicNameValuePair("System", "系统名称"));                params.add(new BasicNameValuePair("LogCategory", "LOG等级"));                params.add(new BasicNameValuePair("Executor", "操作人"));                params.add(new BasicNameValuePair("Action", "发生了什么事"));                httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));                HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);                int stateCode = httpResponse.getStatusLine().getStatusCode();                if (stateCode == 200){                    HttpEntity httpEntity = httpResponse.getEntity();                    result = EntityUtils.toString(httpEntity);                }                if (result.equals("succeed")){                    isSendSucceed = true;                }                Message msg = new Message();                msg.what = 2;                msg.obj = isSendSucceed;                handler.sendMessage(msg);            } catch (Exception e){                e.printStackTrace();            }        }    }
Copy after login
好了,简单的客户端post数据到php服务器端存储的功能已经完成了。


版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage analysis of Executor and ThreadPool in Java parallel programming Usage analysis of Executor and ThreadPool in Java parallel programming Apr 18, 2024 pm 05:33 PM

The Executor interface provides a task execution mechanism, and ThreadPool is its implementation, managing the thread pool to execute tasks. ThreadPool is created using the Executors tool class, such as newFixedThreadPool(), and uses the execute() method to submit tasks. In a practical case, ExecutorService and ThreadPool are used to calculate the sum of squares of numbers to demonstrate the use of parallel programming. Considerations include balancing thread pool size and number of tasks, avoiding exceptions being thrown, and closing ThreadPool after use.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What are the benefits of using the Executor framework in Java concurrent programming? What are the benefits of using the Executor framework in Java concurrent programming? May 08, 2024 pm 06:45 PM

The advantages provided by the Executor framework in Java concurrent programming include: simplified thread management and simplified thread operations through thread pool management. Flexible task management provides customized methods to control task execution. Scalability and performance, automatically adjusting thread pool size to support large-scale task processing. Simplify error handling and improve application stability by centrally handling task execution exceptions.

How to use Java thread pool Executor How to use Java thread pool Executor Apr 28, 2023 am 10:01 AM

Thread Pool Class Diagram The most commonly used Executors implementation to create a thread pool and use threads mainly uses the classes provided in the above class diagram. The class diagram above includes an Executor framework, which is a framework that schedules execution and controls asynchronous tasks based on a set of execution strategy calls. The purpose is to provide a mechanism that separates task submission from how tasks are run. It contains three executor interfaces: Executor: a simple interface for running new tasks ExecutorService: extends Executor, adding methods for managing the executor life cycle and task life cycle ScheduleExcutorService: extends ExecutorSe

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失怎么解决 图片消失怎么解决 Jun 13, 2016 am 10:09 AM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 Jun 13, 2016 am 10:24 AM

为什么我在php上写的这个代码,在浏览器上什么都不显示啊

See all articles