Android Asynchronous Http Client

Gradle Dependency

dependencies {
    ...
    implementation 'com.loopj.android:android-async-http:1.4.9'
}

AsyncClient

AsyncHttpClient client = new AsyncHttpClient();
client.get(URL, params, new JsonHttpResponseHandler(){
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        super.onSuccess(statusCode, headers, response);
    }
});

Build Request Params

RequestParams params = new RequestParams();
params.put("lat", latitude);
params.put("lon", longitude);
params.put("appid", APP_ID);

k-fold crossvalidation with sklearn

from sklearn.model_selection import KFold

kf = KFold(n_splits=2)

kf.split(df_train)

step = 0 # set counter to 0
for train_index, val_index in kf.split(df_train): # for each fold
    step = step + 1 # update counter

    print('Step ', step)

    features_fold_train = df_train.iloc[train_index, [4, 5]] # features matrix of training data (of this step)
    features_fold_val = df_train.iloc[val_index, [4, 5]] # features matrix of validation data (of this step) 

    target_fold_train = df_train.iloc[train_index, 6] # target vector of training data (of this step)
    target_fold_val = df_train.iloc[val_index, 6] # target vector of validation data (of this step) 

    print("VALIDATE:", val_index)
    print('Dimensions features matrix for validation: ', features_fold_val.shape)
    print("TRAIN:", train_index)
    print('Dimensions features matrix for training: ',features_fold_train.shape, '\n')

Data Science Pipeline

Learning Data Science can be grueling and overwhelming sometimes. When I feel too overwhelmed it’s time to draw a picture. This my current overview of what a data scientist has to do:

Sound and music with pygame

pip install pygame

Sound

import pygame

if __name__ == '__main__':
    pygame.mixer.init()
    sound = pygame.mixer.Sound("./sound.wav")
    channel = sound.play()
    while channel.get_busy():
        pygame.time.wait(100)
        print("Playing...")
    print("Finished.")

 

Music

import pygame

if __name__ == '__main__':
    pygame.mixer.init()
    pygame.mixer.music.load("./sound.mp3")
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.wait(100)  # ms
        print("Playing...")
    print("Finished.")

Getting Location Info in Android

Manifest

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

Activity

 

public static final int REQUEST_CODE = 123;
LocationManager mLocationManager; 
LocationListener mLocationListener;
@Override
protected void onResume(){
    super.onResume();
    getCurrentLocation();
}
private void getCurrentLocation() {
    Log.d("Clima", "getWeatherForCurrentLocation");
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("Clima", "onLocationChanged");
            String longitude = String.valueOf(location.getLongitude());
            String latitude = String.valueOf(location.getLatitude());
            Log.d("Clima", "longitude is: " +longitude);
            Log.d("Clima","latitude is: " + latitude);
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };
    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_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    mLocationManager.requestLocationUpdates(LOCAION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
}

 

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    Log.d("Clima", "onRequestPermissionsResult");
    if (requestCode == REQUEST_CODE) {
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            getCurrentLocation();
        }
    }
}

Passing data between AndroidActivities

Sending data

private void openVideoActivity(String video) {
    Intent newActivity = new Intent(this, PlayerActivity.class);
    newActivity.putExtra("videoId", video);
    startActivity(newActivity);
}

Retrieving data

final String videoID = getIntent().getExtras().getString("videoID");