nayakasu
nayakasu

Reputation: 969

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.

Upvotes: 25

Views: 19725

Answers (3)

Basheer Kohli
Basheer Kohli

Reputation: 164

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.

Upvotes: 5

KishuDroid
KishuDroid

Reputation: 5451

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.

Upvotes: 1

user3242316
user3242316

Reputation: 33

Here is a simple and working solution:

Create following two functions:

public static JSONObject getLocationInfo(String address) {
    StringBuilder stringBuilder = new StringBuilder();
    try {

    address = address.replaceAll(" ","%20");    

    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    stringBuilder = new StringBuilder();


        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}
private static List<Address> getAddrByWeb(JSONObject jsonObject){
    List<Address> res = new ArrayList<Address>();
    try
    {
        JSONArray array = (JSONArray) jsonObject.get("results");
        for (int i = 0; i < array.length(); i++)
        {
            Double lon = new Double(0);
            Double lat = new Double(0);
            String name = "";
            try
            {
                lon = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");

                lat = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
                name = array.getJSONObject(i).getString("formatted_address");
                Address addr = new Address(Locale.getDefault());
                addr.setLatitude(lat);
                addr.setLongitude(lon);
                addr.setAddressLine(0, name != null ? name : "");
                res.add(addr);
            }
            catch (JSONException e)
            {
                e.printStackTrace();

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

    }

    return res;
}

Now Simply replace

geocoder.getFromLocation(locationAddress, 1); 

with

getAddrByWeb(getLocationInfo(locationAddress));  

Upvotes: 0

Related Questions