Error while performing android route : Androidhttpclient: Leak found

Asked

Viewed 72 times

0

Guys I’m making the following mistake :

08-24 22:52:53.529 9872-9880/br.com.anjodarua E/AndroidHttpClient: Leak found
                                                                   java.lang.IllegalStateException: AndroidHttpClient created and never closed
                                                                       at android.net.http.AndroidHttpClient.<init>(AndroidHttpClient.java:156)
                                                                       at android.net.http.AndroidHttpClient.newInstance(AndroidHttpClient.java:142)
                                                                       at br.com.anjodarua.MainActivity$2.run(MainActivity.java:352)

This method is the click of my boot when I search, pass the routes on hand to avoid work

 public void getRouteByGMAV2(View view) throws UnsupportedEncodingException{
        EditText etO = (EditText) findViewById(R.id.edt_origem);
        EditText etD = (EditText) findViewById(R.id.edt_destino);
        String origin = URLEncoder.encode(etO.getText().toString(), "UTF-8");
        String destination = URLEncoder.encode(etD.getText().toString(), "UTF-8");

        getRoute(new LatLng(-20.195403, -40.234478), new LatLng(-20.304596, -40.291813));
    }

error in my getRoute method in the Androidhttoclient line follows

// WEB CONNECTION
    //public void getRoute(final String origin, final String destination){
    public void getRoute(final LatLng origin, final LatLng destination){
        new Thread(){
            public void run(){
                        /*String url= "http://maps.googleapis.com/maps/api/directions/json?origin="
                                + origin+"&destination="
                                + destination+"&sensor=false";*/
                String url= "http://maps.googleapis.com/maps/api/directions/json?origin="
                        + origin.latitude+","+origin.longitude+"&destination="
                        + destination.latitude+","+destination.longitude+"&sensor=false";


                HttpResponse response;
                HttpGet request;
                AndroidHttpClient client = null;
                client = AndroidHttpClient.newInstance("route",MainActivity.this);

                request = new HttpGet(url);
                try {
                    response = client.execute(request);
                    final String answer = EntityUtils.toString(response.getEntity());

                    runOnUiThread(new Runnable(){
                        public void run(){
                            try {
                                //Log.i("Script", answer);
                                list = buildJSONRoute(answer);
                                drawRoute();
                            }
                            catch(JSONException e) {
                                e.printStackTrace();
                            }
                        }
                });

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

However my json comes populated , when it will return the "Lines" it gives the error indicated above , follows the json

 // PARSER JSON
    public List<LatLng> buildJSONRoute(String json) throws JSONException{
        JSONObject result = new JSONObject(json);
        JSONArray routes = result.getJSONArray("routes");

        distance = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("value");

        JSONArray steps = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
        List<LatLng> lines = new ArrayList<LatLng>();

        for(int i=0; i < steps.length(); i++) {
            Log.i("Script", "STEP: LAT: "+steps.getJSONObject(i).getJSONObject("start_location").getDouble("lat")+" | LNG: "+steps.getJSONObject(i).getJSONObject("start_location").getDouble("lng"));


            String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points");

            for(LatLng p : decodePolyline(polyline)) {
                lines.add(p);
            }

            Log.i("Script", "STEP: LAT: "+steps.getJSONObject(i).getJSONObject("end_location").getDouble("lat")+" | LNG: "+steps.getJSONObject(i).getJSONObject("end_location").getDouble("lng"));
        }

        return(lines);
    }




    // DECODE POLYLINE
    private List<LatLng> decodePolyline(String encoded) {

        List<LatLng> listPoints = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
            Log.i("Script", "POL: LAT: "+p.latitude+" | LNG: "+p.longitude);
            listPoints.add(p);
        }
        return listPoints;
    }

1 answer

0

Try when finishing your AndroidHttpClient using client.close() to shut it down.

Androidhttpclient created and Never closed

This message above says that a variable of the type has been created AndroidHttpClient but never closed. I suppose that’s it. Use the code below and adapt your needs:

try {
    HttpResponse response = client.execute(request);
    return new ByteArrayInputStream(new EntityUtils.toByteArray(response.getEntity()));
} catch (IOException e) {
    e.printStackTrace();
} finally {
    client.close();
}

Also check if you are giving permission to INTERNET in his manifest. And when it comes to Android 6.0, check the issues of Control your app permissions.

<uses-permission android:name="android.permission.INTERNET"/> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.