Json 400错误-给定位置无效
我试图通过API检索JSON数据,并将其解析到我的Android中。我正在尝试记录检索到的JSON数据,但我一直收到一个“400错误-给定位置无效”。访问API的参数似乎正确,但我不确定为什么无法检索数据Json 400错误-给定位置无效,json,api,okhttp,Json,Api,Okhttp,我试图通过API检索JSON数据,并将其解析到我的Android中。我正在尝试记录检索到的JSON数据,但我一直收到一个“400错误-给定位置无效”。访问API的参数似乎正确,但我不确定为什么无法检索数据 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); temper
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();
}
}
如果您试图检查发送给
API
的请求以及API
发送给您的请求,那么您应该执行。使用它既简单又容易。首先,我已从您的答案中删除了您的密钥,并将其替换为“secret-key”。DarkSky每天只有1000个请求是免费的,所以有人可以抓取密钥并重用它。你得付钱
我想去重置密钥以避免风险
第二,坐标交换。你现在有
final double latitude = -104.8319;
final double longtitude = 39.7294;
String forecastURL = "https://api.darksky.net/forecast/" + apiKey + "/"
+ latitude + ","
+ longtitude;
这一结果是:
然后是“400,位置无效”,因为世界上没有纬度为-104、经度为39的位置
正确的答案是
final double latitude = 39.7294;
final double longtitude = -104.8319;
那么您的urlString
是:
浏览器中的哪些输出:
{
"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
}
注意,我包含了exclude
查询项以缩短响应以显示示例。删除该部分,您将获得所有响应,包括分钟、每日等字段