首先 要定义几个解析json的方法parseJsonMulti,代码如下: [java] view plain copy private void parseJsonMulti(StringstrResult){ try { Log.v( strResult11 , strResult11= +strResult); int index=strResult.indexOf( [ ); if (index 0 ) strResult=str
首先 要定义几个解析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++){
-
-
-
-
JSONObject jsonObject = wifiMapData.getJSONObject(i);
-
-
int id=Integer.parseInt(jsonObject.getString("id"));
-
-
-
String mac_address = jsonObject.getString("mac_address");
-
-
String wifi_name=jsonObject.getString("wifi_name");
-
-
-
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");
-
-
String wifi_adds = jsonObject.getString("wifi_adds");
-
-
String area = jsonObject.getString("area");
-
-
String location_type = jsonObject.getString("location_type");
-
-
String ap_free = jsonObject.getString("ap_free");
-
-
String category = jsonObject.getString("category");
-
-
String password = jsonObject.getString("password");
-
-
String capabilities = jsonObject.getString("capabilities");
-
-
String user_score = jsonObject.getString("user_score");
-
-
String NW_score = jsonObject.getString("NW_score");
-
}
-
-
} catch (JSONException e) {
-
System.out.println("Jsons parse error !");
-
e.printStackTrace();
-
}
-
}
再定义一个向MySql发送http请求的方法connServerForResult,代码如下:
-
private String connServerForResult(String strUrl) {
-
-
HttpGet httpRequest = new HttpGet(strUrl);
-
String strResult = "";
-
try {
-
-
HttpClient httpClient = new DefaultHttpClient();
-
-
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";
-
-
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;
-
-
}
-
-
-
-
$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++;
-
-
-
}
-
-
-
echo json_encode($arr);
-
-
-
-
-
-
>
-
还有一点需要注意,就是如果你的终端上的操作系统是android4.0以上的,要添加上那一段代码,上一篇有写,这里略过
如此一来,可以从MySql中成功地将数据读取下来