Android通过json向MySQL中读写数据的方法详解【读取篇】
本文实例讲述了Android通过json向MySQL中读取数据的方法。分享给大家供大家参考,具体如下:
首先要定义几个解析json的方法parseJsonMulti,代码如下:
privatevoidparseJsonMulti(StringstrResult){
try{
Log.v("strResult11","strResult11="+strResult);
intindex=strResult.indexOf("[");
if(index>0)
strResult=strResult.substring(index,strResult.length());
Log.v("strResult22","strResult22="+strResult);
wifiMapData=newJSONArray(strResult);
Log.v("wifiMapDataLength",""+wifiMapData.length());
for(inti=0;i<wifiMapData.length();i++){///基站信息处理
///MapDatam=newMapData(1,dLat[5],dLong[5],10,20,300,500,105,"教1",1,1,4);
JSONObjectjsonObject=wifiMapData.getJSONObject(i);
intid=Integer.parseInt(jsonObject.getString("id"));//id
//if(jsonObject.isNull("mac_address"))mac_address="";
Stringmac_address=jsonObject.getString("mac_address");//wifi的mac地址
Stringwifi_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"));//经度
}
Stringlocation_name=jsonObject.getString("location_name");//ssid
Stringwifi_adds=jsonObject.getString("wifi_adds");//wifi地址具体到多少路多少号
Stringarea=jsonObject.getString("area");//北京的什么区
Stringlocation_type=jsonObject.getString("location_type");//地点是个什么类型的,写字楼??
Stringap_free=jsonObject.getString("ap_free");//ap是否免费
Stringcategory=jsonObject.getString("category");//ap是否免费
Stringpassword=jsonObject.getString("password");//ap是否免费
Stringcapabilities=jsonObject.getString("capabilities");//ap是否免费
Stringuser_score=jsonObject.getString("user_score");//ap是否免费
StringNW_score=jsonObject.getString("NW_score");//ap是否免费
}
//tvJson.setText(s);
}catch(JSONExceptione){
System.out.println("Jsonsparseerror!");
e.printStackTrace();
}
}
再定义一个向MySql发送http请求的方法connServerForResult,代码如下:
privateStringconnServerForResult(StringstrUrl){
//HttpGet对象
HttpGethttpRequest=newHttpGet(strUrl);
StringstrResult="";
try{
//HttpClient对象
HttpClienthttpClient=newDefaultHttpClient();
//获得HttpResponse对象
HttpResponsehttpResponse=httpClient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//取得返回的数据
strResult=EntityUtils.toString(httpResponse.getEntity());
}
}catch(ClientProtocolExceptione){
Toast.makeText(Setting.this,
"protocolerror",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}catch(IOExceptione){
Toast.makeText(Setting.this,
"IOerror",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
returnstrResult;
}
然后就是在主程序中调用这两个方法:代码如下
StringstrUrl1="http://192.168.1.2/call_for_wifiMapData.php";
//获得返回的Json字符串
StringstrResult1=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表中与点(lat,lon)距离小于distance的点打包回来
$con=mysql_connect("localhost","root",null);
if(!$con){
die('Couldnotconnect:'.mysql_error());
}
mysql_select_db("a0722152915",$con);
mysql_query("setnames'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);\
echojson_encode($arr);
?>
还有一点需要注意,就是如果你的终端上的操作系统是android4.0以上的,要添加上那一段代码,上一篇《Android通过json向MySQL中读写数据的方法详解【写入篇】》有写,这里略过
如此一来,可以从MySql中成功地将数据读取下来
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。