android向AWS发送数据

时间:2016-07-18 03:42:47

标签: android amazon-web-services

我开始学习使用android客户端向AWS发送实时数据。我用kinesis发送数据。但是,我没有找到示例代码。我尝试根据我对AWS教程的理解来编写代码(http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-kinesis.html)。但是,仍然存在两个问题:1。context.getCachedDir()有错误。我不知道如何设置上下文(智能手机上的目录,或AWS上的目录?)2。受保护的Void doInBackground(Void ... v)未命中返回语句。

是否有任何建议可以找出问题?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 个答案:

答案 0 :(得分:1)

  1. context.getCachedDir()有错误。 - 这是电话上的客户端dir。错误应该表明错误。这是一种标准的Android方法。 https://developer.android.com/reference/android/content/Context.html#getCacheDir()

    1. 您只需返回null;