Error making android route: AndroidHttpClient: Leak found

0

Personal I have the following error:

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 button when I go to search, pass the routes in the 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));
    }

The error in my getRoute method in the line AndroidHttoClient 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();
}

But my json comes populated, when it will return the "lines" it gives the error indicated above, follows 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;
    }
    
asked by anonymous 25.08.2016 / 13:45

1 answer

0

Try finalizing your AndroidHttpClient using client.close() to close it.

  

AndroidHttpClient created and never closed

This message above says that a variváel of type AndroidHttpClient was created but never closed. I suppose that's it. Use the code below and tailor 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 that you are giving INTERNET permission to your manifest . And for Android 6.0, check out Control your app permissions .

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