2

I am retrieving lat long values from a server but when I'm getting the JSON its giving me a null value. this my code where it gets the JSON

private void getJSON(String url) {
        class GetJSON extends AsyncTask<String, Void, String>{
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(MapActivity.this, "Please Wait...",null,true,true);
            }

            @Override
            protected String doInBackground(String... params) {

                String uri = params[0];

                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();

                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    String json;
                    while((json = bufferedReader.readLine())!= null){
                        sb.append(json+"\n");
                    }

                    return sb.toString().trim();

                }catch(Exception e){
                    return null;
                }

            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                q=s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute(url);
    }

My Whole Code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        GPSTracker gps = new GPSTracker(this);

        curLat = gps.getLatitude();
        curLng = gps.getLongitude();

        try {
            // Loading map
            initilizeMap();

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


    }

    private void extractJSON(){
        try {
            JSONObject jsonObject = new JSONObject(q);
            users = jsonObject.getJSONArray(JSON_ARRAY);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void showData() {
        if(i <= users.length()) {
            try {
                JSONObject jsonObject = users.getJSONObject(i);

                double maplat = Double.parseDouble((jsonObject.getString(lat)));
                double maplng = Double.parseDouble((jsonObject.getString(lng)));

                MarkerOptions marker = new MarkerOptions().position(new LatLng(maplat, maplng)).title("Hello Maps");

                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));

                googleMap.addMarker(marker);

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

    private void getJSON(String url) {
        class GetJSON extends AsyncTask<String, Void, String>{
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(MapActivity.this, "Please Wait...",null,true,true);
            }

            @Override
            protected String doInBackground(String... params) {

                String uri = params[0];

                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();

                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    String json;
                    while((json = bufferedReader.readLine())!= null){
                        sb.append(json+"\n");
                    }

                    return sb.toString().trim();

                }catch(Exception e){
                    return null;
                }

            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                q=s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute(url);
    }

    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        MarkerOptions marker = new MarkerOptions().position(new LatLng(curLat, curLng)).title("Hello Maps");

// Changing marker icon
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));

// adding marker
        googleMap.addMarker(marker);

        getJSON(GET_URL);
        extractJSON();
        showData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
}

PHP Code

<?php

        require_once('dbConnect.php');

        $sql = "SELECT * FROM eventinfo";

        $res = mysqli_query($con,$sql);
        $result = array();
        while($row = mysqli_fetch_array($res)) {
            array_push($result,
            array('lat'=>$row[9],'lng'=>$row[10]));
        }
        echo json_encode(array("result"=>$result));
        mysqli_close($con);

?>
droidev
  • 7,352
  • 11
  • 62
  • 94
ErlAl
  • 439
  • 1
  • 7
  • 13
  • @Add `e. printStackTrace ()` in `catch` block then check getting any error and also use logs to check what response getting from server in `sb.toString().trim()` line – ρяσѕρєя K Dec 14 '15 at 04:38
  • 2
    You calls are not synchronized, call `extractJSON()` and `showData()` after the `getJson()` method has finished executing, you can do that in `onPostExecute()` of your inner `AsyncTask` class – Satyen Udeshi Dec 14 '15 at 04:38
  • Im not having any errors now and my JSON is not null. But it isnt plotting the markers even tho it has lat and lng. @edit yea the calls are not synchronized. – ErlAl Dec 14 '15 at 04:49

0 Answers0