Service not available while calling geoCoder.getFr

2019-01-03 09:28发布

I know sometimes google back-end service might not be available.

Hence a solution might be to loop until i get the data.

private class getLocationDetails extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        Log.d("looping", "" + count + "");
        count++;
        double lat = Double.parseDouble(params[0]);
        double lng = Double.parseDouble(params[1]);
        List<Address> addresses = null;
        try {

            Geocoder gCoder = new Geocoder(ImageAndLocationActivity.this,
                    Locale.getDefault());
            addresses = gCoder.getFromLocation(lat, lng, 1);
            Address addr = addresses.get(0);
            user_country = addr.getCountryName();
            user_city = addr.getLocality();
            user_district = addr.getSubAdminArea();

            if (user_city == null) {

                user_city = user_district;
            }
        } catch (Exception e) {

            Log.e("Exception in getLocationDetails - ", e.getMessage());
            return null;
        }

        return "";
    }

    @Override
    protected void onPostExecute(String result) {

        if (result != null) {

            Log.d("user_city = ", "" + user_city);
        } else {

            new getLocationDetails().execute(CurrentLat + "", CurrentLng
                    + "");
        }
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }
}

But i am not able to get the location at all:

LogCat:

02-27 16:29:49.568: D/looping(10966): 110355
02-27 16:29:49.568: E/Exception in getLocationDetails -(10966): Service not Available
02-27 16:29:49.573: D/looping(10966): 110356
02-27 16:29:49.573: E/Exception in getLocationDetails -(10966): Service not Available
02-27 16:29:49.573: D/looping(10966): 110357
02-27 16:29:49.573: E/Exception in getLocationDetails -(10966): Service not Available

and ofcourse i have added all the needed permissions:

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

I am trying this on Samsung Galaxy Note GT-N7000 (4.0.4 version)

Am i missing any settings? related to device or application ? Or this usually happens? If so any better solution to resolve this??

Thank You

14条回答
Melony?
2楼-- · 2019-01-03 10:22

Restart the device and it will fix the issue.

查看更多
欢心
3楼-- · 2019-01-03 10:23

use this trick.

simply edit the project.properties

# Project target
target=Google Inc.:Google APIs:16

The reason is that the Geocoder class is present in the core Android framework, but depends on code contributed by the Google APIs to function properly. Even if your AVD includes the Google APIs, your project still needs to be built against that specific build target.

查看更多
ら.Afraid
4楼-- · 2019-01-03 10:25

I'm using the code that is up (direct access to Google Maps) "merged" with Geocoder code, as shown below (Pay special attention to "try catch"):

...
//address is String
if (address != null) {
    new GeocoderTask().execute(address);
}
...

// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {

    private LatLng latLng;
    private MarkerOptions markerOptions;

    @Override
    protected List<Address> doInBackground(String... locationName) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                addresses = getLocationFromString(locationName[0]);
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {

        if (addresses == null || addresses.size() == 0) {
            Toast.makeText(getBaseContext(), "No Location found",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        // Clears all the existing markers on the map
        googleMap.clear();

        // Adding Markers on Google Map for each matching address
        for (int i = 0; i < addresses.size(); i++) {

            Address address = (Address) addresses.get(i);

            // Creating an instance of GeoPoint, to display in Google Map
            latLng = new LatLng(address.getLatitude(),
                    address.getLongitude());

            String addressText = String.format(
                    "%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address
                            .getCountryName());

            markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(addressText);

            googleMap.addMarker(markerOptions);

            // Locate the first location
            if (i == 0) {
                CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
                CameraUpdate zoom = CameraUpdateFactory.zoomTo(13);

                googleMap.moveCamera(center);
                googleMap.animateCamera(zoom);
            }

        }

    }
}

public static LatLng getLocationFromString(String address)
    throws JSONException {

    HttpGet httpGet = new HttpGet(
        "http://maps.google.com/maps/api/geocode/json?address="
                + URLEncoder.encode(address, "UTF-8") + "&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(stringBuilder.toString());

    double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
        .getJSONObject("geometry").getJSONObject("location")
        .getDouble("lng");

    double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
        .getJSONObject("geometry").getJSONObject("location")
        .getDouble("lat");

    return new LatLng(lat, lng);
}

    public static List<Address> getStringFromLocation(double lat, double lng)
    throws ClientProtocolException, IOException, JSONException {

    String address = String
        .format(Locale.ENGLISH,                                 "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=true&language="
                        + Locale.getDefault().getCountry(), lat, lng);
    HttpGet httpGet = new HttpGet(address);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    List<Address> retList = null;

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

    JSONObject jsonObject = new JSONObject(stringBuilder.toString());

    retList = new ArrayList<Address>();

    if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
    JSONArray results = jsonObject.getJSONArray("results");
    for (int i = 0; i < results.length(); i++) {
        JSONObject result = results.getJSONObject(i);
        String indiStr = result.getString("formatted_address");
        Address addr = new Address(Locale.getDefault());
        addr.setAddressLine(0, indiStr);
        retList.add(addr);
    }
    }

    return retList;
}

This worked excellent for me because when Geocoder not work, i use direct access to Google Maps.

Cheers!

查看更多
祖国的老花朵
5楼-- · 2019-01-03 10:27

Service not Available - Geocoder Android when i get this error in some cooked roms i wrote this library i hope could be useful. https://github.com/dnocode/gapis

查看更多
孤傲高冷的网名
6楼-- · 2019-01-03 10:28

I have also had trouble with this error. It happened when I updated my device to Marshmallow recently.

If I reboot, it works once, but then will fail, and not work at all thereafter.

I created an AsyncTask like other people, that only returns the address from the first result of the json response.

To use the code below, call it constructed with your api key, and you use a Location object as input to execute the AsyncTask. You can import Location with the following. import android.location.Location; You will have to get the current Location with the LocationManager, by requesting an update to it.

    new ReverseGeoCodeTask(GOOGLE_API_KEY).execute(location);

Make sure you replace the api key with your own, and also make sure you enable it in the google cloud console. That is where you manage all the google apis for your particular project.

Copy this class as an Inner Class in the Activity that you are needing the reverse geocoded address.

/**
 * Reverse geocode request - takes a Location in as parameters,
 * and does a network request in the background to get the first address in
 * json response. The address is returned in the onPostExecute so you
 * can update the UI with it
 */

private class ReverseGeoCodeTask extends AsyncTask<Location, Void, String>{

    private final static String GEOCODE_API_ENDPOINT_BASE = "https://maps.googleapis.com/maps/api/geocode/json?latlng=";
    private final static String JSON_PROPERTY_RESULTS = "results";
    private final static String JSON_PROPERTY_FORMATTED_ADDRESS = "formatted_address";
    private final static String JSON_PROPERTY_REQUEST_STATUS = "status";
    private final static String STATUS_OK = "OK";
    private String apiKey;

    public ReverseGeoCodeTask(final String apiKey){
        this.apiKey = apiKey;
    }

    @Override
    protected String doInBackground(Location... params) {

        if(apiKey == null){
            throw new IllegalStateException("Pass in a geocode api key in the ReverseGeoCoder constructor");
        }

        Location location = params[0];
        String googleGeocodeEndpoint = GEOCODE_API_ENDPOINT_BASE + location.getLatitude() + "," + location.getLongitude() + "&key=" + apiKey;
        Log.d(TAG, "Requesting gecoding endpoint : " + googleGeocodeEndpoint);
            try {
                URL url = new URL(googleGeocodeEndpoint);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                JSONObject json = new JSONObject(result.toString());
                String requestStatus = json.getString(JSON_PROPERTY_REQUEST_STATUS);
                if(requestStatus.equals(STATUS_OK)){
                    JSONArray results = json.getJSONArray(JSON_PROPERTY_RESULTS);
                    if(results.length() > 0){
                        JSONObject result1 = results.getJSONObject(0);
                        String address =  result1.getString(JSON_PROPERTY_FORMATTED_ADDRESS);
                        Log.d(TAG, "First result's address : " + address );
                        return  address;


                    }
                    else{
                        Log.d(TAG, "There were no results.");
                    }
                }
                else{
                    Log.w(TAG, "Geocode request status not " + STATUS_OK + ", it was " + requestStatus );
                    Log.w(TAG, "Did you enable the geocode in the google cloud api console? Is it the right api key?");
                }


            }catch ( IOException | JSONException e){

                e.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPostExecute(String address) {
        super.onPostExecute(address);
        if(address != null){
            // update the UI here with the address, if its not null
            originEditText.setText(address);
        }
        else{
            Log.d(TAG, "Did not find an address, UI not being updated");
        }

    }
}
查看更多
forever°为你锁心
7楼-- · 2019-01-03 10:30

Workaround using direct access to google maps:

    public static LatLng getLocationFromString(String address)
        throws JSONException {

    HttpGet httpGet = new HttpGet(
            "http://maps.google.com/maps/api/geocode/json?address="
                    + URLEncoder.encode(address, "UTF-8") + "&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(stringBuilder.toString());

    double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

    double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    return new LatLng(lat, lng);
}

    public static List<Address> getStringFromLocation(double lat, double lng)
        throws ClientProtocolException, IOException, JSONException {

    String address = String
            .format(Locale.ENGLISH,                                 "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=true&language="
                            + Locale.getDefault().getCountry(), lat, lng);
    HttpGet httpGet = new HttpGet(address);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    List<Address> retList = null;

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

    JSONObject jsonObject = new JSONObject(stringBuilder.toString());

    retList = new ArrayList<Address>();

    if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
        JSONArray results = jsonObject.getJSONArray("results");
        for (int i = 0; i < results.length(); i++) {
            JSONObject result = results.getJSONObject(i);
            String indiStr = result.getString("formatted_address");
            Address addr = new Address(Locale.getDefault());
            addr.setAddressLine(0, indiStr);
            retList.add(addr);
        }
    }

    return retList;
}
查看更多
登录 后发表回答