Simple interaction between Android client and PHP service

Source: Internet
Author: User
Tags php server

The simple interaction between the Android client and the Php+mysql+apache build allows the log information to be stored.

The realization principle is that the Android client sends the request, passes the server log information, the server receives these, connects the database to store, and returns the stored state to the client.

Server-side:

First in MySQL built a testlog database, there is a log_info table, recorded logcategory,system,executor,action and other information.

Create a new PHP project Testlog in the PHP virtual directory, creating the conn.php and log_deal.php files.


<?phpinclude_once (' conn.php ');//echo ' $_post receive:</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";    }? >
Server Setup is complete.

Android Client:

Layout feel free to write it.

Here's the main code:

Class Sendloghandler implements runnable{@Override public void Run () {try {Stri                ng 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", "Systems Name"));                Params.add (New Basicnamevaluepair ("Logcategory", "Log Rank"));                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 (). Execute (HttpRequest);                int statecode = Httpresponse.getstatusline (). Getstatuscode (); if (statecode = =) {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 (); }        }    }
Well, the simple client post data to PHP server-side storage has been completed.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple interaction between Android client and PHP service

Related Article

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.