Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geocoder.getFromLocation function throws "Timed out waiting for server response" exception

I am trying to get location of user by starting IntentService from MainActivity. Inside the service i try to reverse geocode the location inside a try block but when i catch the exception and print it says "Timed out waiting for server response" exception.But a few times I have got the location.so i think there is nothing wrong with my code.But it won't be useful if it throws exception 8 times out of 10.So can you suggest some thing to avoid this.

like image 937
nayakasu Avatar asked May 13 '14 17:05

nayakasu


2 Answers

http://maps.googleapis.com/maps/api/geocode/json?latlng=lat,lng&sensor=true

Geocoder having the bug of time out waiting for server response .In alternate to that you can hit this request from your code to get the json response of location address of respective lat,lng.

like image 111
Basheer Kohli Avatar answered Oct 17 '22 06:10

Basheer Kohli


I have suffered from, and got error due to unavailability of internet connection.

Please check have assign internet permission:

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

And double check that your internet is ON or not.

And for the sake of getting response I write my code in AsyncTask like below:

 class GeocodeAsyncTask extends AsyncTask<Void, Void, Address> {

        String errorMessage = "";

        @Override
        protected void onPreExecute() {

            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Address doInBackground(Void ... none) {
            Geocoder geocoder = new Geocoder(DashboardActivity.this, Locale.getDefault());
            List<Address> addresses = null;


            double latitude = Double.parseDouble(strLatitude);
            double longitude = Double.parseDouble(strLongitude);

            try {
                addresses = geocoder.getFromLocation(latitude, longitude, 1);
            } catch (IOException ioException) {
                errorMessage = "Service Not Available";
                Log.e(TAG, errorMessage, ioException);
            } catch (IllegalArgumentException illegalArgumentException) {
                errorMessage = "Invalid Latitude or Longitude Used";
                Log.e(TAG, errorMessage + ". " +
                        "Latitude = " + latitude + ", Longitude = " +
                        longitude, illegalArgumentException);
            }


            if(addresses != null && addresses.size() > 0)
                return addresses.get(0);

            return null;
        }

        protected void onPostExecute(Address address) {
            if(address == null) {
                progressBar.setVisibility(View.INVISIBLE);

                tvcurrentLOc.setText(errorMessage);
            }
            else {
                String addressName = "";
                for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    addressName += "," + address.getAddressLine(i);
                }
                progressBar.setVisibility(View.INVISIBLE);

                tvcurrentLOc.setText(addressName);
            }
        }
    }

Hope it will help.

like image 1
KishuDroid Avatar answered Oct 17 '22 05:10

KishuDroid