Skip to content

Instantly share code, notes, and snippets.

@rohanmendon
Created August 28, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohanmendon/e843807cfc5463ae0beceac3e5bc4353 to your computer and use it in GitHub Desktop.
Save rohanmendon/e843807cfc5463ae0beceac3e5bc4353 to your computer and use it in GitHub Desktop.
Android code to post data to that Kinesis Stream
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.kinesis.kinesisrecorder.KinesisRecorder;
File directory = getApplicationContext().getCacheDir();
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(), "Identity pool ID", Regions.US_WEST_2);
KinesisRecorder kinesisRecorder = new KinesisRecorder(directory, Regions.US_WEST_2, credentialsProvider);
String kinesisStreamName = "test2-kinesis-data-stream";
JSONObject json = new JSONObject();
try {
json.accumulate("rpm", vehicleStats.getRpm());
json.accumulate("speed", vehicleStats.getSpeed());
json.accumulate("fuel", vehicleStats.getFuel());
json.accumulate("distance", vehicleStats.getDistance());
json.accumulate("vin", vehicleStats.getVin());
json.accumulate("dtc", vehicleStats.getDtc());
json.accumulate("timestamp", vehicleStats.getTimestamp());
kinesisRecorder.saveRecord(json.toString(), kinesisStreamName);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... v) {
try {
kinesisRecorder.submitAllRecords();
} catch (AmazonClientException ace) {
Log.e(TAG, "Error = " + ace.getMessage());
}
return null;
}
}.execute();
} catch (Exception e) {
Log.e(TAG, "Error = " + e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment