我想从Android Studio中调用open天气地图API,但它有时让我401 Unauthorized
或400 Bad request
我曾试图改变API密钥,但它不工作,尽管我也试图改变天气的URL,但它给了我没有发现我可以”找不到错误,但它似乎有一些错误的网站上。
大段引用
// 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";
大段引用
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);
}
大段引用
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();
}
});
}