I'll walk you through the key points, bear with me for a while.
What I'm Doing
I'm getting the location from location manager and using Geocoder to get city name.
How I'm Doing it In on location changed function, I crate a new thread and make a new address list to get addresses from geocoder in a try catch block.
The Code
public void onLocationChanged(Location location) {
Log.d("onLocationChanged","Before if");
if (location != null){
Log.d("if location not null","start");
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Log.d("TAG", "onLocationChanged: Location : " +location);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Log.d("TAG", "onLocationChanged address: "+addresses);
city = addresses.get(0).getLocality();
search(city);
return;
} catch (IOException e) {
progressDialog.dismiss();
Log.d("IOException","at geocoder " + e.getMessage());
//Toast.makeText(getContext(),"Slow Connection...Try Later.",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
thread.start();
}
else {
progressBar.setVisibility(View.INVISIBLE);
Log.d("Location is null", "else");
Toast.makeText(fl.getContext(),"Cannot Access Current Location...",Toast.LENGTH_LONG).show();
return;
}
}
What's Happening
Based on the logs, location is being fetched but grpc failed exception occurs when geocoder.getFromLocation function is invoked.
The Logs
D/onLocationChanged: Before if
D/if location not null: start
D/TAG: onLocationChanged: Location : Location[gps 33.565110,73.016913 hAcc=5 et=+18822d15h39m59s954ms etAcc=+1ms alt=0.0 vel=0.0 bear=0.0 vAcc=1 sAcc=1 bAcc=30 {Bundle[mParcelledData.dataSize=96]}]
D/IOException: at geocoder grpc failed
W/System.err: java.io.IOException: grpc failed
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.rehnsehan.rehnsehan.ExploreActivity$5.run(ExploreActivity.java:194)
at java.lang.Thread.run(Thread.java:923)
W/ManagedChannelImpl: [{0}] Failed to resolve name. status={1}
W/Firestore: (23.0.1) [WatchStream]: (51c23b2) Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
at io.grpc.internal.DnsNameResolver.resolveAll(DnsNameResolver.java:436)
at io.grpc.internal.DnsNameResolver$Resolve.resolveInternal(DnsNameResolver.java:272)
at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:228)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Note: This function was working just fine for the last month and did not throw any exception.
Thanks, Any suggestions or feedback is appreciated.