【问题标题】:Open weather map gives me Bad request打开天气地图给了我错误的请求
【发布时间】:2019-02-06 20:28:24
【问题描述】:

我正在尝试从 android studio 调用打开的天气地图 api,但它有时会给我401 Unauthorized400 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();
        }
    });

}

【问题讨论】:

    标签: java api android-studio openweathermap


    【解决方案1】:

    经度的键是lon 而不是long。查看 api 规范here

    使用这个:

    params.put("lon" , longitude);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-04
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多