I have been learning android studio via a course online and I'm having a problem. I am making a weather app using a dependency of James Smith's Android Asynchronous Http Client and I am unable to get the location update. I am only getting onResume() and onPaused() methods being called in the logcat. I've also used coarse location with gps provider as well as network provider and fine location with gps and network provider. My mentor suggested to use Network provider and get the last location to make things work and it did but when I compiled the exact same code the next day, it didn't work. I have mentioned the permission in the manifest file for Internet and Access fine location. My mobile (OnePlus 3) also has location and internet enabled and the permission from mobile is already granted. Also android studio gives me an error regarding the [Location lastLocation = mlocationManager.getLastKnownLocation(LOCATION_PROVIDER)] part. Also I'm fairly new to coding so if anyone can explain it to me in simple words I would really appreciate it.
This is the code.
public class WeatherController extends AppCompatActivity {
// Constants:
final int REQUEST_CODE = 123;
final String WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather";
// App ID to use OpenWeather data
final String APP_ID = "a_______________96";
// Time between location updates (5000 milliseconds or 5 seconds)
final long MIN_TIME = 5000;
// Distance between location updates (1000m or 1km)
final float MIN_DISTANCE = 1000;
// TODO: Set LOCATION_PROVIDER here:
String LOCATION_PROVIDER = LocationManager.NETWORK_PROVIDER;
// Member Variables:
TextView mCityLabel;
ImageView mWeatherImage;
TextView mTemperatureLabel;
// TODO: Declare a LocationManager and a LocationListener here:
LocationManager mLocationManager;
LocationListener mLocationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather_controller_layout);
// Linking the elements in the layout to Java code
mCityLabel = (TextView) findViewById(R.id.locationTV);
mWeatherImage = (ImageView) findViewById(R.id.weatherSymbolIV);
mTemperatureLabel = (TextView) findViewById(R.id.tempTV);
ImageButton changeCityButton = (ImageButton) findViewById(R.id.changeCityButton);
// TODO: Add an OnClickListener to the changeCityButton here:
}
// TODO: Add onResume() here:
@Override
protected void onResume() {
super.onResume();
Log.d("Clima", "onResume() called");
Log.d("Clima", "Getting weather for current location");
getWeatherForCurrentLocation();
}
// TODO: Add getWeatherForNewCity(String city) here:
// TODO: Add getWeatherForCurrentLocation() here:
private void getWeatherForCurrentLocation() {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Location lastLocation = mLocationManager.getLastKnownLocation(LOCATION_SERVICE);
Log.d("Clima", "onLocationChanged() callback received");
String longitude = String.valueOf(location.getLongitude());
String latitude = String.valueOf(location.getLatitude());
Log.d("Clima", "Longitude is: " + longitude);
Log.d("Clima", "Latitude is: " + latitude);
RequestParams params = new RequestParams();
params.put("lat", latitude);
params.put("lon", longitude);
params.put("appid", APP_ID);
letsDoSomeNetworking(params);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Log.d("Clima","onStatusChanged() called");
}
@Override
public void onProviderEnabled(String s) {
Log.d("Clima", "onProviderEnabled() called");
}
@Override
public void onProviderDisabled(String s) {
Log.d("Clima", "onProviderDisabled() callback received");
}
};
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_CODE) {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Log.d("Clima", "onRequestPermissionsResult() permission granted!");
getWeatherForCurrentLocation();
} else {
Log.d("Clima", "onRequestPermissionsResult() permission denied!");
}
}
}
// TODO: Add letsDoSomeNetworking(RequestParams params) here:
private void letsDoSomeNetworking(RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(WEATHER_URL, params , new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.d("Clima","Success! JSON: " + response.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e , JSONObject response) {
Log.e("Clima", "Fail" + e.toString());
Log.d("Clima", "StatusCode: " + statusCode);
Toast.makeText(WeatherController.this, "Request failed", Toast.LENGTH_SHORT).show();
}
});
}
// TODO: Add updateUI() here:
// TODO: Add onPause() here:
@Override
protected void onPause(){
super.onPause();
Log.d("Clima", "onPause() callback received ");
Log.d("Clima","Weather data gathering stopped.");
}
}