簡體   English   中英

android發送數據到AWS

[英]android sending data to AWS

我開始學習使用android客戶端將實時數據發送到AWS。 我使用運動學來發送數據。 但是,我沒有找到示例代碼。 我嘗試根據對AWS教程的理解來編寫代碼,以開始使用( http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-kinesis.html )。 但是,仍然存在兩個問題:1. context.getCachedDir()有錯誤。 我不知道如何設置上下文(智能手機上的目錄或AWS上的目錄?)2.受保護的Void doInBackground(Void ... v)錯過返回語句。

有什么建議可以解決這個問題嗎?

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()發生錯誤。 -這是電話上的客戶端目錄。 該錯誤應表明出了什么問題。 這是標准的Android方法。 https://developer.android.com/reference/android/content/Context.html#getCacheDir()

    1. 您可以簡單地返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM