【问题标题】:400 error - The given location is invalid400 错误 - 给定的位置无效
【发布时间】:2017-12-09 09:52:05
【问题描述】:

我正在尝试通过 API 检索 JSON 数据并将其解析到我的 Android 中。我正在尝试记录检索到的 JSON 数据,但我不断收到 "400 error - given location is invalid." 访问 API 的参数似乎正确,但我不确定为什么我无法检索数据。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    temperatureLabel = (TextView) findViewById(R.id.temperatureLabel);
    timeLabel = (TextView) findViewById(R.id.timeLabel);
    refreshButton = (ImageView) findViewById(R.id.refreshImage);

    final double latitude = -104.8319;
    final double longtitude = 39.7294;

    refreshButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getWeatherForecast(latitude, longtitude);
        }
    });

    getWeatherForecast(latitude, longtitude);
}

    public void getWeatherForecast(double latitude, double longtitude) {

    String apiKey = "SECRET-KEY;
    String forecastURL = "https://api.darksky.net/forecast/" + apiKey + "/" + latitude + ","
            + longtitude;

    if (isNetworkAvailable()) {

        //Build and HTTP request
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(forecastURL).build();

        //Make an Api call
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        alertUserError();
                    }
                });
            }
            @Override
            public void onResponse(Response response) throws IOException {
            try {

                String jsonData = response.body().string();
                Log.e(TAG, "JASON DATA" + jsonData);

                if (response.isSuccessful()) {
                    mcurrentWeather = getCurrentWeatherDetails(jsonData);
                    // You want to update the display In the UI.
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            updateDisplay();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (IOException e) {
               Log.e(TAG, "Exception Caught");
            } catch (JSONException e) {
               Log.e(TAG, "JSONexception Caught");
            }
        }
    });
} else {
    alertUserError();
    }
}

【问题讨论】:

    标签: json api okhttp


    【解决方案1】:

    如果您尝试检查发送给API 的请求以及API 发送给您的请求,那么您应该实现OkHttp logging interceptor。它简单易用。

    【讨论】:

      【解决方案2】:

      首先,我已从您的答案中删除了您的密钥,并将其替换为“SECRET-KEY”。 DarkSky 每天只有 1000 个免费请求,因此有人可以抓住该密钥并重复使用它。您必须为此付费。

      我会去https://darksky.net/dev/account重置密钥以避免风险。

      其次,你的坐标被交换了。你目前有

      final double latitude = -104.8319;
      final double longtitude = 39.7294;
      String forecastURL = "https://api.darksky.net/forecast/" + apiKey + "/" 
              + latitude + ","
              + longtitude;
      

      那个结果:

      https://api.darksky.net/forecast/SECRET-KEY/-104.8319,39.7294?exclude=minutely,hourly,daily,flags,alerts

      然后是“400,位置无效”,因为世界上没有纬度 -104 和经度 39 的位置。

      正确的是

      final double latitude = 39.7294;
      final double longtitude = -104.8319;
      

      那么你的urlString 是:

      https://api.darksky.net/forecast/SECRET-KEY/39.7294,-104.8319?exclude=minutely,hourly,daily,flags,alerts

      在浏览器中输出:

      {
      "latitude":39.7294,
      "longitude":-104.8319,
      "timezone":"America/Denver",
      "currently":{
          "time":1583068320,
          "summary":"Mostly Cloudy",
          "icon":"partly-cloudy-night",
          "nearestStormDistance":9,
          "nearestStormBearing":145,
          "precipIntensity":0,
          "precipProbability":0,
          "temperature":37.32,
          "apparentTemperature":33.14,
          "dewPoint":18.62,
          "humidity":0.46,
          "pressure":1011.5,
          "windSpeed":5.24,
          "windGust":7.61,
          "windBearing":157,
          "cloudCover":0.87,
          "uvIndex":0,
          "visibility":10,
          "ozone":309},
      "offset":-7
      }
      

      P.S:请注意,我包含了 exclude 查询项以缩短响应以显示示例。删除该部分,您将获得所有响应,包括分钟、每日等字段。

      【讨论】:

        猜你喜欢
        • 2020-05-25
        • 1970-01-01
        • 2021-02-16
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        相关资源
        最近更新 更多