我正在尝试通过API检索JSON数据并将其解析到我的Android中。我正在尝试记录检索到的JSON数据,但我一直收到错误 - 给定位置无效。"访问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 = "2e31c60ad62bae1a7bba53403d1a5a40";
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();
}
}
在这里输入代码
答案 0 :(得分:0)
如果您要检查发送给API
的请求以及API
发送给您的请求,那么您应该实施OkHttp logging interceptor。使用它简单易行。
答案 1 :(得分:0)
首先,我从答案中删除了您的秘密密钥,并将其替换为“ 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;
以下结果:
则为“ 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
}
P.S:注意,我添加了exclude
查询项以缩短响应以显示示例。删除该部分,您将获得所有响应,包括分钟,每天等等。