1

我有一个按钮的活动,我想用它给我一些天气信息在同一个活动(我显示一些textView下的按钮...)。我有这样的事情:如何通过Google Awareness API获取天气信息?

______________________ 
| give me weather info | (button with onClick="addMeteoInfo") 
"""""""""""""""""""""" 
temperature:    (TextView) 
humidity:      (TextView) 

我尝试使用谷歌意识API来获取有关我的当前位置(https://developers.google.com/awareness/android-api/get-started)天气信息,但我有一些麻烦...... ,我有以下步骤:其次是:

1)添加在我的清单文件:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<meta-data android:name="com.google.android.awareness.API_KEY" android:value="@string/google_maps_key" /> 

<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> 

,其中“@字符串/ google_maps_key”是,我已经通过了谷歌开发者控制台获得,然后添加到我的strings.xml字符串文件资源。

2)加入我的build.gradle(模块:应用程序)文件:

compile 'com.google.android.gms:play-services-awareness:9.6.1'

3)在我的活动我:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_nuova_battuta); 
    ... 
    android.content.Context context; 
    GoogleApiClient client = new GoogleApiClient.Builder(context) 
     .addApi(Awareness.API) 
     .build(); 
    client.connect(); 
} 

// onClick method for button "give me weather info" 
public void addMeteoInfo(View view){ 
    richiediInfoMeteo(); 
} 

public void richiediInfoMeteo() { 
    if(!checkLocationPermission()) { 
     return; 
    } 

    Awareness.SnapshotApi.getWeather(mGoogleApiClient) 
      .setResultCallback(new ResultCallback<WeatherResult>() { 
       @Override 
       public void onResult(@NonNull WeatherResult weatherResult) { 

        if (!weatherResult.getStatus().isSuccess()) {  <----HERE 
         Log.e(TAG, "Could not get weather."); 

         return; 
        } 

        Weather weather = weatherResult.getWeather(); 
        Log.e("Tuts+", "Temp: " + weather.getTemperature(Weather.FAHRENHEIT)); 
        Log.e("Tuts+", "Feels like: " + weather.getFeelsLikeTemperature(Weather.FAHRENHEIT)); 
        Log.e("Tuts+", "Dew point: " + weather.getDewPoint(Weather.FAHRENHEIT)); 
        Log.e("Tuts+", "Humidity: " + weather.getHumidity()); 

        //converto i valori interi ottenuti dall'API Google play services, in stringhe 
        String temperaturaStr = Double.toString(weather.getTemperature(Weather.CELSIUS)); 
        String temperaturaPercepitaStr = Double.toString(weather.getFeelsLikeTemperature(Weather.CELSIUS)); 
        String puntoRugiadaStr = Double.toString(weather.getDewPoint(Weather.CELSIUS)); 
        String umiditaStr = Double.toString(weather.getHumidity()); 

        //prelevo id visualizzatori di tali valori nel layout 
        mostraTemperatura = (TextView) findViewById(R.id.temperatura); 
        mostraTemperaturaPercepita = (TextView) findViewById(R.id.temperaturaPercepita); 
        mostraPuntoRugiada = (TextView) findViewById(R.id.puntoDiRugiada); 
        mostraUmidita = (TextView) findViewById(R.id.umidita); 

        // setto i visualizzatori ai valori convertiti in stringhe 
        mostraTemperatura.setText(temperaturaStr); 
        mostraTemperaturaPercepita.setText(temperaturaPercepitaStr); 
        mostraPuntoRugiada.setText(puntoRugiadaStr); 
        mostraUmidita.setText(umiditaStr); 

       } 
      }); 
} 


private boolean checkLocationPermission() { 
    //se permessi non disponibili allora li richiedo tramite finestra dialog 
    if(!hasLocationPermission()) { 
     Log.e("Tuts+", "Does not have location permission granted"); 
     requestLocationPermission(); 
     return false; 
    } 
    //altrimenti se possiede già permessi 
    return true; 
} 

private boolean hasLocationPermission() { 
    return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED; 
} 

private void requestLocationPermission() { 
    ActivityCompat.requestPermissions(
      NuovaBattutaActivity.this, 
      new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }, 
      RICHIESTA_PERMESSO_INFO_METEO); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    switch (requestCode) { 
     case RICHIESTA_PERMESSO_INFO_METEO: 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       //granted 
       richiediInfoMeteo(); 
      } else { 
       Log.e("Tuts+", "Location permission denied."); 
      } 
     default: 
      break; 
    } 
} 

所以,当我跑我的应用程序AVD我总是在控制台上留言: 无法得到天气。即方法weatherResult.getStatus()。isSuccess()(在richiediInfoMeteo()方法中)始终返回false。 我错在哪里?我该如何处理?

在此先感谢。

+0

有什么建议吗? – Fobi

回答

1

尝试使用weatherResult.getStatus().getStatusCode()函数。这会返回一个可能的错误代码,可能是looked up here

可能的问题是位置服务未在您的设备上启用。启用这些并再试一次,这可能会解决您的问题。我有一个类似的问题(错误代码7503)。