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

Source: Internet
Author: User
Tags tojson

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

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:

<span style="font-family:Microsoft YaHei;font-size:14px;">Gson gson = new Gson();gson.toJson(user))</span>
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:

<Span style = "font-family: Microsoft YaHei; font-size: 14px; ">/*** Description: Send user data to the background * @ param user */<span style =" font-family: Times New Roman; "> public static void SaveDataToPhp (User user User) {Gson gson = new Gson (); String url =" http://bmhjqs.sinaapp.com/ChzuAppDate/chzu_user_save.php "; HttpPost httpRequest = new HttpPost (url ); list <NameValuePair> params = new ArrayList <NameValuePair> (); 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 () ;}</span> </span>
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:

<Span style = "font-family: Microsoft YaHei; font-size: 14px;"> // receives json data from the client. <span style = "font-family: times New Roman; "> $ json_string = $ _ POST [" userJson "]; $ user = json_decode ($ json_user); if (ini_get (" magic_quotes_gpc ") = "1") {$ json_string = stripslashes ($ json_string) ;}</span> $ user = json_decode ($ json_string, true ); // The parameter 'true' must be added; otherwise, PHP does not consider $ user as an array. </span>
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:

<Span style = "font-family: Microsoft YaHei; font-size: 14px;"> // save it to the database <span style = "font-family: Times New Roman; "> $ 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 ';} </span>
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!

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.