2016-07-18 39 views
0

我开始学习使用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)未命中返回语句。android将数据发送到AWS

是否有任何建议,找出问题?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); 
    } 


} 

回答