This article illustrates the way Android reads data to MySQL via JSON. Share to everyone for your reference, specific as follows:
First, you define several methods for parsing JSON Parsejsonmulti, the code is as follows:
private void Parsejsonmulti (String strresult) {try {log.v ("strResult11", "strresult11=" +strresult);
int Index=strresult.indexof ("[");
if (index>0) strresult=strresult.substring (Index, Strresult.length ());
LOG.V ("strResult22", "strresult22=" +strresult);
Wifimapdata = new Jsonarray (strresult);
LOG.V ("Wifimapdatalength", "" "+wifimapdata.length ()); for (int i = 0; i < wifimapdata.length (); i++) {///Base station information processing///mapdata m=new MapData (1, dlat[5], dlong[5], 10, 20, 300
, 500, 105, "Teach 1", 1, 1, 4);
Jsonobject Jsonobject = Wifimapdata.getjsonobject (i); int Id=integer.parseint (jsonobject.getstring ("id"));
ID//if (Jsonobject.isnull ("mac_address")) mac_address= ""; String mac_address = jsonobject.getstring ("mac_address");//wifi mac address string wifi_name=jsonobject.getstring ("Wifi_ Name "); SSID if (!jsonobject.isnull ("lat") &&!jsonobject.isnull ("Lon")) {lat= double.valueof (Jsonobject.getstrin G ("lat"));/Latitude Lon=double.valUeof (jsonobject.getstring ("Lon"));//Longitude} String location_name=jsonobject.getstring ("Location_name"); SSID string wifi_adds = jsonobject.getstring ("Wifi_adds"),//wifi address specific to how many routes number String area = Jsonobject.getstrin
G ("area"); What district of Beijing String Location_type = jsonobject.getstring ("Location_type"); where is the type of office?
String ap_free = jsonobject.getstring ("Ap_free"),//ap free string category = Jsonobject.getstring ("category"); string password = jsonobject.getstring ("password");//ap free string capabilities = Jsonobject.getstring ("Capabil Ities ");//ap whether free string user_score = jsonobject.getstring (" User_score ");//ap whether free string nw_score = jsonobject.ge
Tstring ("Nw_score");//ap free}//Tvjson.settext (s);
catch (Jsonexception e) {System.out.println ("Jsons parse Error!");
E.printstacktrace ();
}
}
To define a method to send HTTP requests to MySQL Connserverforresult, the code is as follows:
private string Connserverforresult (string strurl) {
//HttpGet object
httpget HttpRequest = new HttpGet (strurl);
String strresult = "";
try {
//HttpClient object
httpclient httpclient = new Defaulthttpclient ();
Obtain HttpResponse object
httpresponse httpresponse = Httpclient.execute (HttpRequest);
if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {
//Get the returned data
strresult = Entityutils.tostring (Httpresponse.getentity ());
}
catch (Clientprotocolexception e) {
toast.maketext (setting.this,
"protocol error", Toast.length_short). Show ();
E.printstacktrace ();
} catch (IOException e) {
toast.maketext (setting.this,
IO error, Toast.length_short). Show ();
E.printstacktrace ();
}
return strresult;
}
Then you call the two methods in the main program: The code is as follows
String STRURL1 = "http://192.168.1.2/call_for_wifiMapData.php";
Gets the returned JSON string string
strResult1 = Connserverforresult (STRURL1);
LOG.V ("StrResult1", STRRESULT1);
Parsejsonmulti (STRRESULT1);
Just a few words, PHP also to replace your own file path,
The PHP code is as follows:
<?php $jsonArrayString = $_post["jsonarraystring"];
$jsonString = $_post["jsonstring"];
$objArray =json_decode ($jsonArrayString, true);
$obj =json_decode ($jsonString);
$lon = (float) $obj->lon;
$lat = (float) $obj->lat;
$distance = (float) $obj->distance; if ($lat ==null| |
$lat ==0) {$lat = 39.990132;
$lon = 116.332224;
$distance = 100000;
////the dot (lat,lon) distance in the cell table is smaller than distance $con = mysql_connect ("localhost", "root", null);
if (! $con) {die (' could not connect: '. Mysql_error ());
} mysql_select_db ("a0722152915", $con);
mysql_query ("Set names ' UTF8 '");
$sqlfind = "SELECT * from ' WiFi '";
$resultFind = mysql_query ($sqlfind, $con);
$length =mysql_num_rows ($resultFind);
$arr =array ();
$j = 0;
for ($i =0; $i < $length; $i + +) {$row = Mysql_fetch_array ($resultFind);
$arr [$j] [' id '] = $row [' id '];
$arr [$j] [' mac_address ']= $row [' mac_address '];
$arr [$j] [' Wifi_name ']= $row [' Wifi_name '];
$arr [$j] [' Lat ']= $row [' Gps_lat ']; $arr [$j] [' Lon ']= $row [' Gps_lon '];
$arr [$j] [' Location_name ']= $row [' Location_name '];
$arr [$j] [' Wifi_adds ']= $row [' Wifi_adds '];
$arr [$j] [' area ']= $row [' area '];
$arr [$j] [' Location_type ']= $row [' Location_type '];
$arr [$j] [' Ap_free ']= $row [' Ap_free '];
$arr [$j] [' Category ']= $row [' Category '];
$arr [$j] [' Password ']= $row [' Password '];
$arr [$j] [' Capabilities ']= $row [' capabilities '];
$arr [$j] [' User_score ']= $row [' User_score '];
$arr [$j] [' Nw_score ']= $row [' Nw_score '];
$j + +;
//print_r ($arr); \ Echo Json_encode ($arr);?>
It's also important to note that if you have an operating system on your terminal that is android4.0 above, add that piece of code, the previous "Android through JSON to read and write data in MySQL" in the details of the "write article" has written, here skip
In this way, data can be successfully read from MySQL
For more information on Android-related content readers can view the site: "The Android operation JSON format Data Skills summary", "Android Database Operating skills summary", "Android Programming activity Operating Skills summary", " Android File Operation Tips Summary, "Android programming development of the SD card operation method Summary", "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and " A summary of the usage of Android controls
I hope this article will help you with the Android program.