Search in sources :

Example 1 with Data

use of com.linroid.weather.model.Data in project steps by linroid.

the class MainActivity method onAddCity.

@Override
public void onAddCity(String cityName) {
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("添加中....");
    dialog.show();
    WeatherApp app = (WeatherApp) getApplication();
    WeatherService service = app.getWeatherService();
    service.getDataAsync(cityName, new Callback<Data>() {

        @Override
        public void success(Data data, Response response) {
            Result result = data.getResults().get(0);
            getContentResolver().insert(DataContract.Weather.CONTENT_URI, result.toContentValues());
            // triggerSync();
            Toast.makeText(MainActivity.this, R.string.add_city_success, Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            Toast.makeText(MainActivity.this, R.string.add_city_fail, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Response(retrofit.client.Response) WeatherService(com.linroid.weather.data.WeatherService) Data(com.linroid.weather.model.Data) ProgressDialog(android.app.ProgressDialog) Result(com.linroid.weather.model.Result) RetrofitError(retrofit.RetrofitError)

Example 2 with Data

use of com.linroid.weather.model.Data in project steps by linroid.

the class SyncAdapter method onPerformSync.

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    try {
        Cursor cursor = provider.query(DataContract.Weather.CONTENT_URI, null, null, null, null);
        Timber.d("cursor count:%d", cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            Weather weather = Weather.fromCursor(cursor);
            Data data = weatherService.getData(weather.getCurrentCity());
            if (data.getResults() != null && data.getResults().size() > 0) {
                Uri uri = DataContract.Weather.CONTENT_URI.buildUpon().appendPath(String.valueOf(weather.getId())).build();
                Timber.i("update uri: %s", uri.toString());
                ContentValues values = data.getResults().get(0).toContentValues();
                ContentProviderOperation operation = ContentProviderOperation.newUpdate(uri).withValues(values).build();
                operations.add(operation);
                Timber.d("获取 %s 城市天气成功", data.getResults().get(0).getCurrentCity());
            } else {
                Timber.e("(获取 %s 城市天气失败 %d)Status:%s, message:%s", weather.getCurrentCity(), data.getError(), data.getStatus(), data.getMessage());
            }
            syncResult.stats.numUpdates++;
        }
        Timber.i("同步了%d条数据", syncResult.stats.numUpdates);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
Also used : Weather(com.linroid.weather.model.Weather) ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Data(com.linroid.weather.model.Data) Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) Uri(android.net.Uri)

Aggregations

Data (com.linroid.weather.model.Data)2 ProgressDialog (android.app.ProgressDialog)1 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1 WeatherService (com.linroid.weather.data.WeatherService)1 Result (com.linroid.weather.model.Result)1 Weather (com.linroid.weather.model.Weather)1 ArrayList (java.util.ArrayList)1 RetrofitError (retrofit.RetrofitError)1 Response (retrofit.client.Response)1