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

Source: Internet
Author: User
Tags tojson

Suddenly think of such a function, the user login with a client, the client did the following two things, one is to jump page, return personal information; the second is to return the information to the server, and the server saves the data in the database. So that the user's personal information will be obtained!

Without further ado, hurry up!

Just as my SAE beans have not consumed, I intend to use PHP to do the backstage!

Client-to-server delivery is now more popular than passing JSON strings! (as you've learned about JSON before), Android wraps the data in JSON format, and then sends it to the PHP backend via httpclient, PHP gets the JSON string based on the property name, and then parses it, and the last-saved (MySQL) process is like this.

The first step: The Android client encapsulates JSON format data

First of all the data you want to transmit into JSON format data, you can use the JSON package or Gson, I use the Gson, I want to transfer the user object, the code is as follows:

<span style= "Font-family:microsoft yahei;font-size:14px;" >gson Gson = new Gson (); Gson.tojson (user)) </span>
conversion format is very simple, do not do too much introduction

Step two: Write an async method in the login return thread (of course, you want to call Async at any time, I put it on the back of the login information to trigger the asynchronous task), the asynchronous task calls HttpClient send the request method, the code is as follows:

<span style= "Font-family:microsoft yahei;font-size:14px;" >/** * * Description Send user data to Background * @param user */<span style= "font-family:times New Roman;" >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<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.execute (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>
at this point, the data begins to be sent to PHP.

Step three: Receive JSON data

In PHP, the value is obtained by using the parameter key, and the code is as follows:

<span style= "Font-family:microsoft yahei;font-size:14px;" >//accepts 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") {$JSO n_string = stripslashes ($json _string);} </span> $user = Json_decode ($json _string, true);//must add parameter ' true ', otherwise PHP does not think $user is an array </span>
Note that needs to be noted in the note, to this, you can use Array[key] method to obtain the value;

Fourth step: Save the data

I saved the data in the MySQL database under SAE with the following code:

<span style= "Font-family:microsoft yahei;font-size:14px;" >//start saving to 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 if the database exists by ID Isexit = "query statement"; $result = mysql_query ($isExit), if (Mysql_num_rows ($result) < 1) {$sql = "insert statement ..."; mysql_query (' Set Nam Es utf-8 '); mysql_query ($sql); Echo ' State_ok ';} Else{echo ' State_exist ';} Mysql_close ($link);} else {echo ' State_db_fail ';} </span></span>
Fifth step: Test


The test succeeds, the data can be saved normally!





If the objective feeling is useful, click to praise ... I'll try harder.

If there is a wrong place also please point out, I will correct!

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

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.