简体   繁体   中英

android sending data to AWS

I start to learn use android client to send real time data to AWS. I use kinesis to send data. However, I did not find a sample codes. I try to write the codes based on my understanding from AWS tutorial to start ( http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-kinesis.html ). However, there are still two problems: 1. context.getCachedDir() has an error. I do not know how to set the context(a dir on smartphone, or a dir on AWS?) 2. protected Void doInBackground(Void... v) miss return statement.

Is there any suggestion to figure out the problem?THX

public class MainActivity extends AppCompatActivity {

   CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
         getApplicationContext(), // Context
         "us-east-1:75d540bf-08c8-42fc-87a1-xxxx", // Identity Pool ID
         Regions.US_EAST_1 // Region
   );


   // working directory for the recorder
   File directory = context.getCachedDir();
   // AWS Firehose region
   Regions region = Regions.US_WEST_2;
   // initialize a credentials provider
   AWSCredentialsProvider provider = new CognitoCachingCredentialsProvider(
         context,
         "us-east-1:75d540bf-08c8-42fc-87a1-xxxxx",
         Regions.US_EAST_1);

   KinesisFirehoseRecorder firehoseRecorder = new KinesisFirehoseRecorder(
         directory, region, provider);


   // save some strings
   String streamName = "my_stream"; // Firehose delivery stream name



   @Override
   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  .setAction("Action", null).show();
         }
      });



      firehoseRecorder.saveRecord("Hello world!\n", streamName);
      firehoseRecorder.saveRecord("Streaming data to S3 via Firehose is easy.\n", streamName);

      // send previously save data to Amazon Firehose
      // Note: submitAllRecords() makes network calls, so wrap it in an AsyncTask.

      new AsyncTask<Void, Void, Void>() {
         @Override
         protected Void doInBackground(Void... v) {
            try {

               firehoseRecorder.submitAllRecords();

            } catch (AmazonClientException ace) {
               // error occurs.
            }
         }
      }.execute();


   }



   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }

      return super.onOptionsItemSelected(item);
   }


}
  1. context.getCachedDir() has an error. - This is client side dir on the phone. The error should indicate what is wrong. It is a standard Android method. https://developer.android.com/reference/android/content/Context.html#getCacheDir()

    1. You can simply return null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM