33

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

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • No. I've seen outages for a few minutes. Try rebooting your device? – Reno Feb 27 '13 at 11:36
  • 1
    `isPresent()` returns `True` always. Yet it says `Service not available`. But after restarting the device, it works fine. Weird. How will a user know that :( – Archie.bpgc Feb 27 '13 at 12:03
  • Was there ever a better solution then restarting? I am having the same issue. In testing we cam across this issue on two phones and rebooting fixes the problem. I don't think getting the user to reboot their phone is a very good solution. Is there another object alternative to the GeoCoder Object? – JohnathanKong Nov 29 '13 at 22:16
  • 1
    @JohnathanKong Use this `http://maps.google.com/maps/api/geocode/json?address=" + address + "&ka&sensor=false` as a fall back – Archie.bpgc Nov 30 '13 at 05:40

17 Answers17

69

The actual reason why Geocoder was not working is because the NetworkLocator was killed in action. Probably due to less memory or maybe you used the Task Manager to kill all services?

I'm not sure but this is a guess. I've seen this before. Last year I wrote a reconnect mechanism to load the NetworkLocator.apk and bind to the GeocoderService. I think this change has not merged into JellyBean so this problem persists.

It can be only solved by reboot. (The NetworkLocationService is loaded at boot)

Edit: You won't see this problem in JBP or KK, this service is moved into the playstore app .

Reno
  • 33,594
  • 11
  • 89
  • 102
  • 2
    Fine. But I checked other map related apps like **Maps**, **Local**..etc, They all were working fine. May be because I am using basic `android.location.Geocoder` and they might be using `some google service libraries`?? If that's the case I just started using `google-play-services.jar` does that support any such methods? Thank You – Archie.bpgc Feb 27 '13 at 12:53
  • 1
    I mean. if its a problem with `NetworkLocationService` like you said. The other map based apps must have behaved the same way – Archie.bpgc Feb 27 '13 at 13:03
  • 1
    Google Maps has its own inbuilt NLS. That was one of the first things I checked – Reno Feb 27 '13 at 19:14
  • 1
    how can I explicity switch off the `NetworkLocator`? Because I have a work around for my above problem. Now I want to test that. But unable to reproduce the case. – Archie.bpgc Mar 11 '13 at 13:38
  • You can reproduce this by killing running services (I used the linux cmd for process kill). To disable the Network provider you can go to Location Services in Settings. – Reno Mar 12 '13 at 05:10
  • Every time I use the option `Clear memory` from the `Task manager` on my tablet I get this issue. Reboot solves the problem. – Androiderson May 10 '13 at 22:54
  • @Reno is there any method to solve this problem without rebooting the device? Can we reset the _NetworkLocator_ via code? – Umberto Jul 04 '13 at 13:32
  • @UmbySlipKnot The problem is not about resetting, it is about forming connections which are dead after resetting. Which can be done only from Google code. I had made a hack which works with custom binaries, it is not the right solution though. – Reno Jul 05 '13 at 05:17
  • @Reno do you have any idea? I also tried Google Geocoding API but unfortunately this solution doesn't work with mobile connection of some mobile operators: the request always returns OVER_QUERY_LIMIT. Those mobile operators use NAT overloading, assigning the same IP to many devices... So up to now I'm not able to find coordinates of an address with 100% success. And I don't know how to fix this. – Umberto Jul 05 '13 at 07:01
  • In this case you should report this problem on the Google product forums. – Reno Jul 05 '13 at 07:41
  • Best Answer on internet \m/ \m/ – Nabin Khatiwada Dec 09 '21 at 06:27
22

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;
}
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Mugen
  • 8,301
  • 10
  • 62
  • 140
  • Thanks, it solved the problem with my s3 mini wich refused to use the geocoder service. But it only works when used in an async task. – Ruocco May 23 '15 at 14:33
13

Restart the device and it will fix the issue.

Jiju Induchoodan
  • 4,236
  • 1
  • 22
  • 24
3

API will throw a "Service not Available exception" if such service is unavailable on the device. Use method isPresent() to check for the existence of the service.

See also: http://developer.android.com/reference/android/location/Geocoder.html

0x8BADF00D
  • 7,138
  • 2
  • 41
  • 34
SSemashko
  • 1,499
  • 1
  • 12
  • 14
3

The best fix for this problem is to use the same like Google Geocoder class if the original Geocoder fail

List<Address> addresses = null;    
Geocoder geocoder = new Geocoder(this);
addresses = geocoder.getFromLocation(...);
if (addresses == null || addresses.isEmpty())
addresses = MyGeocoder.getFromLocation(...);


import android.location.Address;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.AllClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MyGeocoder {

    public static List<Address> getFromLocation(double lat, double lng, int maxResult) {

        String address = String.format(Locale.ENGLISH, "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language=" + Locale.getDefault().getCountry(), lat, lng);
        HttpGet httpGet = new HttpGet(address);
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(AllClientPNames.USER_AGENT, "Mozilla/5.0 (Java) Gecko/20081007 java-geocoder");
        client.getParams().setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 5 * 1000);
        client.getParams().setIntParameter(AllClientPNames.SO_TIMEOUT, 25 * 1000);
        HttpResponse response;

        List<Address> retList = null;

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String json = EntityUtils.toString(entity, "UTF-8");

            JSONObject jsonObject = new JSONObject(json);

            retList = new ArrayList<Address>();

            if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
                JSONArray results = jsonObject.getJSONArray("results");
                if (results.length() > 0) {
                    for (int i = 0; i < results.length() && i < maxResult; i++) {
                        JSONObject result = results.getJSONObject(i);
                        //Log.e(MyGeocoder.class.getName(), result.toString());
                        Address addr = new Address(Locale.getDefault());
                        // addr.setAddressLine(0, result.getString("formatted_address"));

                        JSONArray components = result.getJSONArray("address_components");
                        String streetNumber = "";
                        String route = "";
                        for (int a = 0; a < components.length(); a++) {
                            JSONObject component = components.getJSONObject(a);
                            JSONArray types = component.getJSONArray("types");
                            for (int j = 0; j < types.length(); j++) {
                                String type = types.getString(j);
                                if (type.equals("locality")) {
                                    addr.setLocality(component.getString("long_name"));
                                } else if (type.equals("street_number")) {
                                    streetNumber = component.getString("long_name");
                                } else if (type.equals("route")) {
                                    route = component.getString("long_name");
                                }
                            }
                        }
                        addr.setAddressLine(0, route + " " + streetNumber);

                        addr.setLatitude(result.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
                        addr.setLongitude(result.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
                        retList.add(addr);
                    }
                }
            }


        } catch (ClientProtocolException e) {
            Log.e(MyGeocoder.class.getName(), "Error calling Google geocode webservice.", e);
        } catch (IOException e) {
            Log.e(MyGeocoder.class.getName(), "Error calling Google geocode webservice.", e);
        } catch (JSONException e) {
            Log.e(MyGeocoder.class.getName(), "Error parsing Google geocode webservice response.", e);
        }

        return retList;
    }
}
sytolk
  • 7,223
  • 3
  • 25
  • 38
1

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.

jithu
  • 706
  • 1
  • 9
  • 13
1

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

dnocode
  • 1,928
  • 1
  • 18
  • 21
1

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!

Artificioo
  • 704
  • 1
  • 9
  • 19
1

You can go to a map app in your phone and clear cached and will find the address work correctly.

0

I had the same geocoder error but non of the above applied. It would not run one of my Android devices. Then I remembered that I had accedently killed some running service. The solution was to remove the battery for some seconds and re-install it. And it worked without changing the code :))

Zekitez
  • 411
  • 3
  • 6
0

Some devices do not have suport for Geocoder, so what you need to do is create your own geocoder.

Basicaly you need create a async task to request google for the address and treat the json response.

Using aquery, i do something like this:

public void asyncJson(String address){
        address = address.replace(" ", "+");

        String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+ address +"&sensor=true";

        aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {

                @Override
                public void callback(String url, JSONObject json, AjaxStatus status) {                        

                        if(json != null){

                                 //here you work with the response json
                                 JSONArray results = json.getJSONArray("results");                               
                                Toast.makeText(context, results.getJSONObject(1).getString("formatted_address"));

                        }else{                                
                                //ajax error, show error code
                                Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
                        }
                }
        });        
}
Bruno Pinto
  • 2,013
  • 3
  • 23
  • 33
0

For the following line

Geocoder gCoder = new Geocoder(context, Locale.getDefault());

Use Context of your Activity and don't use getApplicationContext()

jafarbtech
  • 6,842
  • 1
  • 36
  • 55
0

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");
        }

    }
}
med116
  • 1,526
  • 17
  • 16
0

Had the same issue on Android 6. The problem was in App permissions. Even if the maps works properly you must allow the "Get position" permission in app permissions.

The best case is always to check this permission allowed when you expect to get the place in result.

I use this method to get the full address from place:

public Address getFullAddress(Place place){
    Address address;

    Locale aLocale = new Locale.Builder().setLanguage("en").build();
    Geocoder geocoder = new Geocoder(this, aLocale);

    try {
        List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude,place.getLatLng().longitude, 1);

        address = addresses.get(0);

        return address;

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

    return null;
}
Taras Vovkovych
  • 4,062
  • 2
  • 16
  • 21
-1

I had the same error, add below permissions to resolve it.

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
<uses-permission android:name="android.permission.INTERNET" />
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Murtaza Ashraf
  • 190
  • 1
  • 5
  • Did not solve the problem for me, probably because GeoCoder is simply not available on my Device (Xperia S) or because of some other glitch. – AgentKnopf Aug 31 '14 at 18:15
-1
new Volly_Services(map, "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + mBinding.loc.getText().toString().trim() + "&key=Ap", getActivity()).vollyPostService().continueWithTask(task - > {
    mBinding.progressBaar.setVisibility(View.GONE);

    if (task.getResult() != null) {

        Log.e("<<<", "" + task.getResult());

        JSONObject jsonObject = new JSONObject("" + task.getResult());
        if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
            JSONArray results = jsonObject.getJSONArray("results");
            if (results.length() > 0) {
                mBinding.loc.setVisibility(View.GONE);
                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);
                    addr.setLocality(result.getString("name"));
                    JSONObject geometry = result.getJSONObject("geometry").getJSONObject("location");
                    addr.setLatitude(geometry.getDouble("lat"));
                    addr.setLongitude(geometry.getDouble("lng"));


                    addresses.add(addr);
                }
                adapter = new SerchLocationAdapter(getActivity(), addresses);
                mBinding.serchreg.setAdapter(adapter);
            } else {
                Toast.makeText(getActivity(), "No result found", Toast.LENGTH_LONG).show();
            }

        } else {
            Toast.makeText(getActivity(), "No result found", Toast.LENGTH_LONG).show();
        }
    } else {
        Log.e("<<<<<<", "" + task.getError().getMessage());
        Toast.makeText(getActivity(), task.getError().getMessage(), Toast.LENGTH_LONG).show();
    }
    return null;
});
Iavor
  • 1,997
  • 16
  • 27
-1

I use Volley and it work fine

   private void callAppFromUrl(final String strAddress, final String app, final boolean isGeo) {
    try {
        Volley.newRequestQueue(this).add(new StringRequest(0, String.format("https://www.google.com/maps?q=%s", URLEncoder.encode(strAddress, "UTF-8")), new Response.Listener<String>() {
            public void onResponse(String response) {
                try {
                    try {
                        Matcher m = Pattern.compile("null,null,(\\d+.\\d+),(\\d+.\\d+)").matcher(response);
                        String strLatLong = "";
                        if (m.find()) {
                            strLatLong = m.group(0).replace("null,null,", "");
                        }
                        String[] latlong = strLatLong.split(",");
                        LatLng latLng = new LatLng(Double.parseDouble(latlong[0]), Double.parseDouble(latlong[1]));
                        Log.d("OsK",String.valueOf(latLng));
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Không tìm thấy địa chỉ", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e2) {
                    Toast.makeText(getApplicationContext(), "Không tìm thấy địa chỉ", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            /* class com.cantho.roadtech.MainActivity.AnonymousClass5 */

            @Override // com.android.volley.Response.ErrorListener
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("User-Agent", "Mozilla");
                params.put("Accept-Language", "en-US,en;q=0.8");
                params.put("Referer", "google.com");
                return params;
            }
        });
    } catch (Exception ex) {
    }
}