我正在尝试从android studio调用开放式天气地图api,但有时它会给我401 Unauthorized
或400 Bad request
,但是我尝试更改api密钥却不起作用,但是我也尝试更改了天气网址,但找不到我,我找不到错误,但网站上似乎有问题。
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();
}
});
}