Android [intermediate tutorial] Chapter 10 MySQL + JSON + Android combination

Source: Internet
Author: User

In this chapter, we will focus on introducing the MySQL database server to process JSON data in PHP by handing it over to Android. At first, the students thought that there were a lot of technical terms, everyone should be able to understand. Well, first of all, let's look at the database, MySQL database, and those who have done a website should be familiar with the MySQL database. First, we should create databases, tables, and data in the MySQL database.

Next, we will expose Mysql Data to Android. Here we use the thinkphp template.

Here we will not talk about thinkphp's database connection and so on, and use thinkphp to return data as JSON data.

Here first

Okay. Now, how can I transfer the network JSON data to Android?

Import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Org. JSON. jsonarray; import Org. JSON. jsonobject; import android. app. activity; import android. OS. bundle; import android. widget. listview; import android. widget. simpleadapter; import COM. kang. HTTP. httpconnectionutil; import COM. kang. HTTP. httpconnectionutil. httpconnectioncallback; import COM. kang. HTTP. httpconnectionutil. httpmethod; public class jsonfromphpdemo extends activity {private listview; private simpleadapter adapter; protected list <person> persons; private person; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. xml_handler); listview = (listview) findviewbyid (R. id. xml_list); try {// getadapter ();} catch (exception e) {e. printstacktrace () ;}}/*** custom method to obtain data from the network and put the data into listview */private void getadapter () {// URL string url = "http: // 192.168.1.105/phpdemo/index. PHP/index/Show "; // network connection method described in the previous lesson httpconnectionutil connutil = new httpconnectionutil (); connutil. asyncconnect (URL, httpmethod. post, new httpconnectioncallback () {@ overridepublic void execute (string response) {persons = jsontolist (response); setinadapter (); listview. setadapter (adapter) ;}}) ;}/ *** add data to listview */protected void setinadapter () {list <Map <string, string> lists = new arraylist <Map <string, string> (); // converts data from persons to arraylist <Map <string, string> medium // string>, because simpleadapter uses this type of data to adapt Map <string, string> map; For (person P: Persons) {map = new hashmap <string, string> (); map. put ("ID", p. GETID (); map. put ("status", p. getstatus (); map. put ("name", p. getname (); map. put ("tool", p. gettool (); map. put ("Number", p. getnumber (); lists. add (MAP) ;}// keystring [] From = {"ID", "status", "name", "tool", in hashmap <string, string> ", "Number"}; // idint [] to = {R. id. item_id, R. id. item_status, R. id. item_name, R. id. item_tool, R. id. item_number}; adapter = new simpleadapter (this, lists, R. layout. handler_list_item, from, );} /*** convert the JSON string to the list <person> data ** @ Param response * input JSON string * @ return */protected list <person> jsontolist (string response) {list <person> List = new arraylist <person> (); try {// convert a string to a JSON array jsonarray array = new jsonarray (response ); // array length int length = array. length (); For (INT I = 0; I <length; I ++) {// convert each array to a JSON object jsonobject OBJ = array. getjsonobject (I); person = new person (); person. setid (obj. getstring ("ID"); person. setstatus (obj. getstring ("status"); person. setname (obj. getstring ("name"); person. settool (obj. getstring ("tool"); person. setnumber (obj. getstring ("Number"); list. add (person) ;}return list;} catch (exception e) {e. printstacktrace ();} return NULL ;}}

Here we have integrated the content of the previous chapters, including network data transmission and JSON processing. I hope you will study it carefully. Thank you.

The server is hosted on your computer. The website is http: // 192.168.1.105/phpdemo/index. PHP/index/show, do not write as localhost, need to write specific IP Address

Download source code: MySQL + JSON + android

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.