如何从Mysql中检索php中的数组并将其绑定到android

时间:2015-10-07 10:14:45

标签: json

我想从php中的mysql中检索一些数据并将其存储在数组中,然后在Android中我想使用该数据。例如,我想检索其职业ID = 1的多个人的位置(比方说),然后在Android中我想在地图上显示这些位置。我不知道该怎么做。我有以下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 个答案:

答案 0 :(得分: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"}]}