0

I want to retrieve some data from mysql in php and store it in array and then in Android I want to use that data. For example I want to retrieve the location of multiple people whose profession id = 1 (let's say) and then in Android I want to show that locations on map. I don't know how to do this. I have the following PHP and Android files which don't work. Please help.

<?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);

and android activity

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 Answer 1

1

Dear imdad: The Problem lies in you json file.

 {"[]":{"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"}}

The Object is empty give it some name like here is response. Change it as:

{"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"}]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.