android java.io.IOException: Service not Available

Description: I am using Google maps API V2.I have implemented Android Reverse Geocoding at touched location.

Problem: It throws exception on

  • how append date build to versionNameSuffix on gradle
  • Get the width of the parent layout
  • Extending RelativeLayout, and overriding dispatchDraw() to create a zoomable ViewGroup
  • how to use DownloadManager in ArrayAdapter
  • How do I take advantage of Android's “Clear Cache” button
  • Android : What should I do instead of using a deprecated function(getwidth() )?
  •  try {
         addresses = geocoder.getFromLocation(latitude, longitude,1);}
     catch (IOException e)
         {
         e.printStackTrace();
         if(AppConstants.DEBUG)Log.v(AppConstants.DEBUG_TAG," e.printStackTrace(): "+e.getMessage());
         }
    

    I am receiving latitude and longitude values correct, but i can’t understand why it throws exception and i have also done Google search but it couldn’t help.

    Can anybody please explain in details??

    Related posts:

    Android: What's the difference between Activity.runOnUiThread and View.post?
    AAPT2 compile failed: invalid dimen on Android 3.0 Canary 1 on Windows
    How to get a contact's facebook id or url from native contacts / content resolver?
    how to convert double to 2 number after the dot?
    Android java.net.SocketException:socket failed: EACCES (Permission denied)
    How to start a dialog (like alarm dimiss /snooze) that can be clicked without unlocking the screen
  • How to set editable true/false EditText in Android programmatically?
  • How to set text to a text view from a string.xml and normal string at a time
  • Writeable and Executable location on Android
  • Android canvas: draw transparent circle on image
  • EditText Values in range
  • Android MediaPlayer Stop and Play
  • 3 Solutions collect form web for “android java.io.IOException: Service not Available”

    As stated in this Android issue 38009 – Geocoder throwing exception: IOException: Service not Available Rebooting the Device will solve the problem

    If you don’t want to reboot the device use this

         public  JSONObject getLocationFormGoogle(String placesName) {
    
        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();
    
        try {
            response = client.execute(httpGet);
            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) {
    
            e.printStackTrace();
        }
    
        return jsonObject;
    }
    
    public  LatLng getLatLng(JSONObject jsonObject) {
    
        Double lon = new Double(0);
        Double lat = new Double(0);
    
        try {
    
            lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");
    
            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");
    
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return new LatLng(lat,lon);
    
    }
    
    
    
    LatLng Source =getLatLng(getLocationFormGoogle(placesName));
    

    I modified @Ani solution to get city name from lat long parameters:

    public static String getLocationCityName( double lat, double lon ){
        JSONObject result = getLocationFormGoogle(lat + "," + lon );
        return getCityAddress(result);
    }
    
    protected static JSONObject getLocationFormGoogle(String placesName) {
    
        String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
        HttpGet httpGet = new HttpGet(apiRequest);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();
    
        try {
            response = client.execute(httpGet);
            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) {
    
            e.printStackTrace();
        }
    
        return jsonObject;
    }
    
    protected static String getCityAddress( JSONObject result ){
        if( result.has("results") ){
            try {
                JSONArray array = result.getJSONArray("results");
                if( array.length() > 0 ){
                    JSONObject place = array.getJSONObject(0);
                    JSONArray components = place.getJSONArray("address_components");
                    for( int i = 0 ; i < components.length() ; i++ ){
                        JSONObject component = components.getJSONObject(i);
                        JSONArray types = component.getJSONArray("types");
                        for( int j = 0 ; j < types.length() ; j ++ ){
                            if( types.getString(j).equals("locality") ){
                                return component.getString("long_name");
                            }
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        return null;
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.