我正在使用Google的FusedLocationAPI和YahooAPI获取我当前的位置(来自fusedlocationAPI)并将其发送到yahooAPI并获取我所在位置的天气。
在onCreate方法中,我从yahooCom.refresh(slocation)方法获取天气。但是,我希望每次调用onlocationUpdates()方法时都会得到天气。但是当我尝试这样做时,它就不再适应天气了。
基本上,我发送给YahooCom类的Slocation String的第一个值可以工作,但是当我更改变量值并再次发送它时,它不起作用(不更新天气)
如何在位置更新时获取天气更新?我是一个初学的android编码器,所以请原谅我的问题,如果它是愚蠢的。
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, YahooComCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here I am finding all views by id etc etc...
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
locationRequest = new LocationRequest();
locationRequest.setInterval(LOCATION_REQUEST_INTERVAL);
locationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST);
locationRequest.setPriority(locationRequest.PRIORITY_HIGH_ACCURACY);
yahooCom = new YahooCom(this);
//yahooCom.refresh(slocation); works here the first time
}
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationUpdates();
}
private void requestLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_READ_FINE_LOCATION);
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
latitudeTextView.setText(String.valueOf(mLatitude));
longitudeTextView.setText(String.valueOf(mLongitude));
slocation = ""+Double.toString(mLatitude)+","+Double.toString(mLongitude)+"";
yahooCom.refresh(slocation);
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
@Override
protected void onResume() {
super.onResume();
if(googleApiClient.isConnected())
requestLocationUpdates();
}
@Override
protected void onPause() {
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}
@Override
public void serviceSuccess(Channel channel) {
code.setText("Code: "+Integer.toString(channel.getItem().getCondition().getCode()));
city.setText(channel.getLocation().getCity());
temperature.setText(Integer.toString(channel.getItem().getCondition().getTemperature())+" \u00B0 C");
condition.setText(channel.getItem().getCondition().getDescription());
}
}
修改
以下是来自YahooCom类的refresh()方法的代码:
public void refresh(final String location){
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
yql = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\") and u=\"C\"", slocation);
endPoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(yql));
try {
URL url = new URL(endPoint);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null){
result.append(line);
}
return result.toString();
} catch (Exception e) {
exception = e;
}
return null;
}
@Override
protected void onPostExecute(String s) {
if(s == null && exception != null){
yahooComCallback.serviceFailure(exception);
return;
}
try {
JSONObject data = new JSONObject(s);
JSONObject query = data.optJSONObject("query");
int count = query.optInt("count");
if(count == 0){
yahooComCallback.serviceFailure(new LocationException("Location does not exist, or no weather information was found for this location"));
return;
}
Channel channel = new Channel();
channel.populate(query.optJSONObject("results").optJSONObject("channel"));
yahooComCallback.serviceSuccess(channel);
} catch (JSONException e) {
yahooComCallback.serviceFailure(e);
}
}
}.execute(location);
}