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 listView;
Private SimpleAdapter adapter;
Protected List <Person> persons;
Private Person person;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. xml_handler );
ListView = (ListView) findViewById (R. id. xml_list );
Try
{
// Adaptive Method
GetAdapter ();
} Catch (Exception e)
{
E. printStackTrace ();
}
}
/**
* Custom method: get data from the network and put the data into the 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 ()
{
@ Override
Public 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> ();
// Convert the data in persons to ArrayList <Map <String, String>
// String>, because SimpleAdapter needs to adapt to this type of data
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 );
}
// Key in HashMap <String, String>
String [] from = {"id", "status", "name", "tool", "number "};
// Control ID in list_item.xml
Int [] 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 a Json string to 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 into 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 );
} Www.2cto.com
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
From the column kangkangz4