Android通過json向MySQL中讀寫數(shù)據(jù)的方法詳解【讀取篇】
本文實(shí)例講述了Android通過json向MySQL中讀取數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
首先 要定義幾個解析json的方法parseJsonMulti,代碼如下:
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++){///基站信息處理
///MapData m=new MapData(1, dLat[5], dLong[5], 10, 20, 300, 500, 105, "教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地址
String wifi_name=jsonObject.getString("wifi_name"); //ssid
if(!jsonObject.isNull("lat")&&!jsonObject.isNull("lon")){
lat= Double.valueOf(jsonObject.getString("lat"));//緯度
lon=Double.valueOf(jsonObject.getString("lon"));//經(jīng)度
}
String location_name=jsonObject.getString("location_name"); //ssid
String wifi_adds = jsonObject.getString("wifi_adds");//wifi地址 具體到多少路多少號
String area = jsonObject.getString("area");//北京的什么區(qū)
String location_type = jsonObject.getString("location_type");//地點(diǎn)是個什么類型的,寫字樓??
String ap_free = jsonObject.getString("ap_free");//ap是否免費(fèi)
String category = jsonObject.getString("category");//ap是否免費(fèi)
String password = jsonObject.getString("password");//ap是否免費(fèi)
String capabilities = jsonObject.getString("capabilities");//ap是否免費(fèi)
String user_score = jsonObject.getString("user_score");//ap是否免費(fèi)
String NW_score = jsonObject.getString("NW_score");//ap是否免費(fèi)
}
// tvJson.setText(s);
} catch (JSONException e) {
System.out.println("Jsons parse error !");
e.printStackTrace();
}
}
再定義一個向MySql發(fā)送http請求的方法connServerForResult,代碼如下:
private String connServerForResult(String strUrl) {
// HttpGet對象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient對象
HttpClient httpClient = new DefaultHttpClient();
// 獲得HttpResponse對象
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的數(shù)據(jù)
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;
}
然后就是在主程序中調(diào)用這兩個方法:代碼如下
String strUrl1 = "http://192.168.1.2/call_for_wifiMapData.php";
//獲得返回的Json字符串
String strResult1 = connServerForResult(strUrl1);
Log.v("strResult1",strResult1);
parseJsonMulti(strResult1);
只有幾句話而已,php同樣要替換成你自己的文件路徑,
php代碼如下:
<?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;
}
////將cell表中與點(diǎn)(lat,lon)距離小于distance的點(diǎn)打包回來
$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);
?>
還有一點(diǎn)需要注意,就是如果你的終端上的操作系統(tǒng)是android4.0以上的,要添加上那一段代碼,上一篇《Android通過json向MySQL中讀寫數(shù)據(jù)的方法詳解【寫入篇】》有寫,這里略過
如此一來,可以從MySql中成功地將數(shù)據(jù)讀取下來
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android發(fā)送GET與POST請求的DEMO詳解
本篇文章是對Android發(fā)送GET與POST請求的DEMO進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android 使用Vitamio打造自己的萬能播放器(8)——細(xì)節(jié)優(yōu)化
本文主要介紹Android Vitamio開發(fā)播放器,這里給大家提供了一些小的細(xì)節(jié)優(yōu)化,更加完善播放器的功能,希望能幫助有需要的小伙伴2016-07-07
Android自定義View繪制貝塞爾曲線中小紅點(diǎn)的方法
貝塞爾曲線的本質(zhì)是通過數(shù)學(xué)計算的公式來繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學(xué)公式和驗(yàn)證,那些偉大的數(shù)學(xué)家已經(jīng)證明過了,所以就只講講Android開發(fā)中的運(yùn)用吧2023-02-02
Android獲取手機(jī)系統(tǒng)版本等信息的方法
這篇文章主要介紹了Android獲取手機(jī)系統(tǒng)版本等信息的方法,涉及Android獲取手機(jī)版本中各種常見信息的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL
這篇文章主要為大家介紹了Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android TextView Marquee的應(yīng)用實(shí)例詳解
這篇文章主要介紹了Android TextView Marquee的應(yīng)用實(shí)例詳解的相關(guān)資料,這里說明使用方法及簡單實(shí)例和注意實(shí)現(xiàn),需要的朋友可以參考下2017-08-08

