I am trying to call open weather map api from android studio but it sometimes gives me 401 Unauthorized
or 400 Bad request
I have tried to change api key but it does not work tho I also tried to change the weather url but it gives me not found I can't find the error but it seems that there is something wrong on the website.
Blockquote
// 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 = "776a27857ef2d1df5e018d2bdde968d4";
// 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;
private static final String TAG = "WeatherController";
Blockquote
private void getWeatherForCurrentUser() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged() callback recieved");
String longitude = String.valueOf(location.getLongitude());
String latitude = String.valueOf(location.getLatitude());
Log.d(TAG, "onLocationChanged: " + longitude);
Log.d(TAG, "onLocationChanged: " + latitude);
RequestParams params = new RequestParams();
params.put("lat" , latitude);
params.put("long" , longitude);
params.put("appid" , APP_ID);
letsDoSomeNetworking(params);
}
Blockquote
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(TAG, "onSuccess: " + response.toString());
}
@Override
public void onFailure(int statusCode , Header[] headers , Throwable e , JSONObject response){
Log.e(TAG, "onFailure: " + e.getMessage().toString() );
Log.d(TAG, "onFailure: " + statusCode);
Toast.makeText(WeatherController.this, "Request Failed" + e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
The key for longitude is lon
not long
. Check the api specs here.
Use this:
params.put("lon" , longitude);