我正在尝试使用Forecast API开发应用。但是这个api只允许通过纬度和经度从一个城市获取数据。这是我的代码
final double latitude=21.0260;
final double longtitude=105.8471;
String forecastUrl="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longtitude;
。此代码将仅从河内市返回数据。如何只使用这个latidude和longtitude从2个或更多城市获取数据? 这是我的被调用的函数,但我不知道如何更改循环,因为我调用了GetForecast()函数:
final double[] latitudes={21.0260,12.0260,21.1260};
final double[] longtitudes={23.0260,42.0260,51.1260};
mRefreshImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getForecast(latitudes,longtitudes);
}
});
getForecast(latitudes,longtitudes);
Log.d(TAG, "MainUI code is running");
}
private void getForecast(double[] latitude,double[] longtitude) {
String apiKey="a424ddd79338808311d0a6197a12731e";
String forecastUrl="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longtitude;
if (isNetWorkAvailable()) {
toogleRefresh();
OkHttpClient client = new OkHttpClient();
Request req = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(req);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
toogleRefresh();
}
}); alertUser();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
toogleRefresh();
}
});
try {
String jsonData=response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mCurrentWeather=getCurrentDetail(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUser();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught:", e);
}catch (JSONException e){
Log.e(TAG, "Exception caught:", e);
}
}
});
}else {
Toast.makeText(this, R.string.error_network_unavailable,Toast.LENGTH_LONG).show();
}
}
updateDetail:
private void updateDisplay() {
mTemperatureLabel.setText(mCurrentWeather.getTemperature() + "");
mHumidityValue.setText(mCurrentWeather.getHumidity() + "");
mTimeLabel.setText( "At "+ mCurrentWeather.getFormattedTime()+" it will be");
mPrecipeValue.setText(mCurrentWeather.getPrecipChance()+"");
mSummaryValue.setText(mCurrentWeather.getSumary());
Drawable drawable=getResources().getDrawable(mCurrentWeather.getIconId());
mIconImageView.setImageDrawable(drawable);
}
getCurrentDetails():
private CurrentWeather getCurrentDetail(String jsonData) throws JSONException{
JSONObject forecast= new JSONObject(jsonData);
String timeZone=forecast.getString("timezone");
Log.i(TAG,"From Json" + timeZone);
JSONObject currently=forecast.getJSONObject("currently");
CurrentWeather currentWeather= new CurrentWeather();
currentWeather.setHumidity(currently.getDouble("humidity"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setIcon(currently.getString("icon"));
currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
currentWeather.setSumary(currently.getString("summary"));
currentWeather.setTemperature(currently.getDouble("temperature")) ;
currentWeather.setTimeZone(timeZone);
Log.d(TAG,currentWeather.getFormattedTime());
return currentWeather;
}
答案 0 :(得分:0)
制作纬度和经度阵列,然后使用for循环获取url。
例如: -
final double[] latitudes={21.0260,12.0260,21.1260};
final double[] longitudes={23.0260,42.0260,51.1260};
String[] forecastUrls;
for(int i;i<latitudes.lenght;i++){
forecastUrls[i] = "https://api.forecast.io/forecast/"+apiKey+"/"+latitudes[i]+","+longtitude[i];
}
已更新: -
mRefreshImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i;i<latitudes.length;i++){
getForecast(latitudes[i],longtitudes[i]);
}
}
});