欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android通过json向MySQL中读写数据的方法详解【读取篇】

程序员文章站 2024-03-31 17:33:04
本文实例讲述了android通过json向mysql中读取数据的方法。分享给大家供大家参考,具体如下: 首先 要定义几个解析json的方法parsejsonmulti,代...

本文实例讲述了android通过json向mysql中读取数据的方法。分享给大家供大家参考,具体如下:

首先 要定义几个解析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"));//经度
     }
     string location_name=jsonobject.getstring("location_name"); //ssid
     string wifi_adds = jsonobject.getstring("wifi_adds");//wifi地址 具体到多少路多少号
     string area = jsonobject.getstring("area");//北京的什么区
     string location_type = jsonobject.getstring("location_type");//地点是个什么类型的,写字楼??
     string ap_free = jsonobject.getstring("ap_free");//ap是否免费
     string category = jsonobject.getstring("category");//ap是否免费
     string password = jsonobject.getstring("password");//ap是否免费
     string capabilities = jsonobject.getstring("capabilities");//ap是否免费
     string user_score = jsonobject.getstring("user_score");//ap是否免费
     string nw_score = jsonobject.getstring("nw_score");//ap是否免费
   }
   // tvjson.settext(s);
  } catch (jsonexception e) {
   system.out.println("jsons parse error !");
   e.printstacktrace();
  }
}

再定义一个向mysql发送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) {
    // 取得返回的数据
    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;
}

然后就是在主程序中调用这两个方法:代码如下

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表中与点(lat,lon)距离小于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);
?>

还有一点需要注意,就是如果你的终端上的操作系统是android4.0以上的,要添加上那一段代码,上一篇《android通过json向mysql中读写数据的方法详解【写入篇】》有写,这里略过

如此一来,可以从mysql中成功地将数据读取下来

更多关于android相关内容感兴趣的读者可查看本站专题:《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程之activity操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。