Created
August 28, 2019 23:43
-
-
Save rohanmendon/e843807cfc5463ae0beceac3e5bc4353 to your computer and use it in GitHub Desktop.
Android code to post data to that Kinesis Stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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