Simple interaction between android client and php service

Source: Internet
Author: User
Simple interaction between the android client and the php service: simple interaction between the android client and php + mysql + apache to achieve log information storage.

The implementation principle is that the android client sends a request to the server log information, the server receives the information, connects to the database for storage, and returns the stored status to the client.

Server:

First, create a database named testlog in mysql, which contains a log_info table that records information such as LogCategory, System, Executor, and Action.

Create a php project testlog under the php virtual directory and create the conn. php and log_deal.php files.


 '; $ 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" ;}?>
The server has been built.

Android client:

You can write the layout as needed.

The following is the main code:

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", "System name"); params. add (new BasicNameValuePair ("LogCategory", "LOG level"); params. add (new BasicNameValuePair ("Executor", "operator"); params. add (new BasicNameValuePair ("Action", "What Happened"); httpRequest. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); HttpResponse httpResponse = new defaulthttpclient(.exe cute (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 ();}}}
All right, the simple client post data to the php server storage function has been completed.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.