Android interacts with PHP, Android transmits JSON data, and PHP accepts and saves data

Source: Internet
Author: User
: This article mainly introduces the interaction between Android and PHP. Android transmits JSON data. PHP accepts and saves data. if you are interested in PHP tutorials, refer to it. Suddenly, when a user logs on to a client, the client performs the following two tasks: Jump to the page and return personal information; and return the information to the server, the server saves the data in the database. In this way, the user's personal information will be obtained!

It's not a matter of time!

I plan to use PHP as the backend even though I have not exhausted my SAE beans!

Client and server transmission are now popular in passing Json strings! (I have learned about Json before). android packs data into Json format and sends the data to the PHP background through Httpclient. php obtains the Json string based on the attribute name and then parses it, this is the final process for saving (MySQL.

Step 1: The Android client encapsulates Json data

First, encapsulate the data you want to transmit into Json format. you can use a Json package or Gson. I use Gson, and I want to transmit a User object. the code is as follows:

Gson gson = new Gson();gson.toJson(user))
The conversion format is very simple.

Step 2: write an asynchronous method in the login return thread (of course, if you want to call the asynchronous method at any time, I will trigger the asynchronous task when returning the login information ), the following code calls Httpclient to send a request in an asynchronous task:

/***** Description send user data to the background * @ param user */public static void SaveDataToPhp (User user) {Gson gson = new Gson (); string url = "http://bmhjqs.sinaapp.com/ChzuAppDate/chzu_user_save.php"; HttpPost httpRequest = new HttpPost (url); List
 
  
Params = new ArrayList
  
   
(); Params. add (new BasicNameValuePair ("userJson", gson. toJson (user); try {HttpEntity httpEntity = new UrlEncodedFormEntity (params, "UTF-8"); httpRequest. setEntity (httpEntity); HttpClient httpClient = new DefaultHttpClient (); HttpResponse httpResponse = httpClient.exe cute (httpRequest); if (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {String result = EntityUtils. toString (httpResponse. getEntity (); Log. I ("save", result);} else {}} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}
  
 
Now, the data is sent to PHP.

Step 3: receive Json data

In php, the Value is obtained through the Key parameter. the code is as follows:

// Accept json data sent from the client $ json_string = $ _ POST ["userJson"]; $ user = json_decode ($ json_user); if (ini_get ("magic_quotes_gpc ") = "1") {$ json_string = stripslashes ($ json_string);} $ user = json_decode ($ json_string, true); // The parameter 'true' must be added ', otherwise, PHP does not consider $ user as an array.
Note that the value is written in the comment. at this point, you can use the array [key] method to obtain the value;

Step 4: Save data

I save the data in the Mysql database under SAE. the code is as follows:

// Start saving to database $ link = mysql_connect (SAE_MYSQL_HOST_M. ':'. SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS); if ($ link) {mysql_select_db (SAE_MYSQL_DB, $ link); // Determine whether the database contains $ isExit = "query statement" based on the ID "; $ result = mysql_query ($ isExit); if (mysql_num_rows ($ result) <1) {$ SQL = "insert statement... "; mysql_query ('set names utf-8 '); mysql_query ($ SQL); echo 'state _ OK';} else {echo 'state _ exist ';} mysql_close ($ link);} else {echo 'state _ DB_FAIL ';}
Step 5: test

The test is successful, and the data can be saved normally!

If it feels useful objectively, click like... I will work harder

If something is wrong, please point it out and I will correct it!

The above describes the interaction between Android and PHP. Android transmits JSON data, and PHP accepts and saves data, including related content. it is helpful to friends who are interested in PHP tutorials.

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.