2015-10-07 48 views
0

我想在php中从mysql中检索一些数据并将其存储在数组中,然后在Android中我想使用该数据。例如,我想检索职业id = 1(比方说)的多个人的位置,然后在Android中我想在地图上显示位置。我不知道该怎么做。我有以下PHP和Android文件不起作用。请帮忙。如何从Mysql中检索数组在php中并将其绑定到android

<?php 
require "config.php"; 

$pro_id=1; 
$sql="SELECT user.first_name, current_location.crtloc_lat,current_location.crtloc_lng FROM user INNER JOIN current_location 
where user.user_id=current_location.user_id AND user.pro_id='$pro_id'"; 

//$sql = "select * from current_location where user_id=76"; 

$res = mysqli_query($con,$sql); 

$result = array(); 

while($row = mysqli_fetch_array($res)){ 
array_push($result, 
array('lat'=>$row[3], 
'lan'=>$row[4] 
)); 
} 

echo json_encode(array("result"=>$result)); 

mysqli_close($con); 

和android活动

public void searchProfession(){ 
     JSONObject myJson = null; 

      try { 
      // http://androidarabia.net/quran4android/phpserver/connecttoserver.php 

      // Log.i(getClass().getSimpleName(), "send task - start"); 

      HttpParams httpParams = new BasicHttpParams(); 

      // 
      HttpParams p = new BasicHttpParams(); 
      // p.setParameter("name", pvo.getName()); 
      // p.setParameter("user", "1"); 

       p.setParameter("profession",SearchProfession); 
      // Instantiate an HttpClient 
      HttpClient httpclient = new DefaultHttpClient(p); 
      String url = "http://abh.netai.net/abhfiles/searchProfession.php"; 
      HttpPost httppost = new HttpPost(url); 

      // Instantiate a GET HTTP method 
      try { 
       Log.i(getClass().getSimpleName(), "send task - start"); 
       //fffffffffffffffffffffffffff 
       httppost.setHeader("Content-type", "application/json"); 
       InputStream inputStream = null; 
       String result = null; 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 

       inputStream = entity.getContent(); 
       // json is UTF-8 by default 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
       // BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
       myJSON=result; 
// return JSON String 


       if(inputStream != null)inputStream.close(); 


       //ffffffffffffffffffffffffffff 
       // 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("user", "1")); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       String responseBody = httpclient.execute(httppost, 
         responseHandler); 
       // Parse 
       JSONObject json = new JSONObject(myJSON); 

       JSONArray jArray = json.getJSONArray("result"); 
       ArrayList<HashMap<String, String>> mylist = 
         new ArrayList<HashMap<String, String>>(); 

       for (int i = 0; i < jArray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        JSONObject e = jArray.getJSONObject(i); 
        String lat = e.getString("lat"); 
        String lan = e.getString("lan"); 
        map.put("lat",lat); 
        map.put("lan",lan); 



        mylist.add(map); 
        Toast.makeText(MapsActivity.this, "your location is"+lat+","+lan, Toast.LENGTH_SHORT).show(); 
       } 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 

       e.printStackTrace(); 
      } catch (IOException e) { 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      // Log.i(getClass().getSimpleName(), "send task - end"); 

     } catch (Throwable t) { 

      Toast.makeText(this, "Request failed: " + t.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

回答

1

亲爱imdad:问题就出在你的JSON文件。

{"[]":{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"}, {"user_id":"76","crtloc_lat":"34.769604","crtloc_lng":"72.361092"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}} 

对象是空的,给它一些名字就像这里是响应。更改为:

{"response":[{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769604","crtloc_lng":"72.361092"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]} 
+0

非常感谢它的工作! –

相关问题