1

I am making google map app. Since Geocoder class returns empty(out of bounds at [0]) i am currently trying the HTTPS Google maps reverse GeoCoding. This is my onClick call:

  public JSONArray getAddress(LatLng latLng) throws IOException {
    URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
    Async async =new Async();
    async.execute(url);
    JSONArray response = async.jsonArray;

    return response;
}

This is the Async subclass:

public class Async extends AsyncTask<URL, Void, JSONArray> {
    public JSONArray jsonArray;
    @Override
    protected JSONArray doInBackground(URL... params) {
        BufferedReader reader;
        InputStream is;

        try {

            StringBuilder responseBuilder = new StringBuilder();
            HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            for (String line; (line = reader.readLine()) != null; ) {
                responseBuilder.append(line).append("\n");
            }
            jsonArray = new JSONArray(responseBuilder.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonArray;
    }

and manifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Onclick method :

@Override
public void onMapClick(LatLng latLng) {
    JSONArray js=null;
    try {
         js = getAddress(latLng);
         mMap.addMarker(new MarkerOptions().position(latLng).title(js.getString(0)).draggable(true));//NullPointer
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException p) {
        //todo

    }

For 4 days now i am trying to get something different than NullPointerException. Any idea what am i doing wrong? Tested on tablet connected in debug mode. Any help will be useful. I am quite desperate.

fixxxera
  • 205
  • 2
  • 10

1 Answers1

1

Since the AsyncTask is a subclass of the Activity, you just need to assign the LatLng to a member variable of the Activity so that it's accessible in the AsyncTask.

Then, add the Marker in onPostExecute().

First, the change to the Activity code. Make a LatLng member variable in addition to the JSONArray:

public class MyMapActivity extends AppCompatActivity implements OnMapReadyCallback
{

    JSONArray js;
    LatLng currLatLng;
    //...............

Then, modify the onMapClick to assign the LatLng to the instance variable:

@Override
public void onMapClick(LatLng latLng) {
    JSONArray js=null;
    try {

         //assign to member variable:
         currLatLng = latLng;

         //don't use the return value:
         getAddress(latLng);

         //remove this:
         //mMap.addMarker(new MarkerOptions().position(latLng).title(js.getString(0)).draggable(true));//NullPointer
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException p) {
        //todo

    }

Then, use onPostExecute to place the marker after you have the response. The other main issue is that the JSON response is a JSONObject that contains a JSONArray inside it. Fixed in the code below:

public class Async extends AsyncTask<URL, Void, JSONArray> {
    public JSONArray jsonArray;

    @Override
    protected JSONArray doInBackground(URL... params) {
        BufferedReader reader;
        InputStream is;
        try {

            StringBuilder responseBuilder = new StringBuilder();
            HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            for (String line; (line = reader.readLine()) != null; ) {
                responseBuilder.append(line).append("\n");
            }
            JSONObject jObj= new JSONObject(responseBuilder.toString());
            jsonArray = jObj.getJSONArray("results");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonArray;
    }


    @Override
    protected void onPostExecute(JSONArray jsonArrayResponse) {
        js = jsonArrayResponse;
        try {
            if (js != null) {
              JSONObject jsFirstAddress = js.getJSONObject(0);
              mMap.addMarker(new MarkerOptions().position(currLatLng).title(jsFirstAddress.getString("formatted_address")).draggable(true));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Note that you don't need to return anything from getAddress(), so just make the return type void and let the AsyncTask do the rest once it's executed:

  public void getAddress(LatLng latLng) throws IOException {
    URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
    Async async =new Async();
    async.execute(url);
    //JSONArray response = async.jsonArray;
    //return response;
  }
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Still getting a NullPointer , again in marker:title(js.getString(0)) This time on onPostExecute where is it now – fixxxera Nov 10 '15 at 17:47
  • 1
    @fixxxera I just ran the code, and realized that the response is a JSONObject that contains a JSONArray, that is why it's null when you try to create a JSONArray with the result. Take a look at the updated answer with the modified AsyncTask. – Daniel Nugent Nov 10 '15 at 19:32
  • THANK YOU MAN! I was halfway there (the Api key was not the one i needed, tried the JsonObj and it gave me the IP error for invalid IP key ) Still you saved my day! THANK YOU! – fixxxera Nov 10 '15 at 19:45