2012-05-08 95 views
0

我是Android上的新手,需要開發一個操縱EC2和S3(亞馬遜網絡服務)的應用程序。我在VirtualBox上的Ubuntu 10.04上使用Eclipse Indigo。我已經安裝SDK for Android,創建了一個新項目,添加了這些罐子:使用Android操作Amazon EC2的困難

-aws-android-sdk-1.2.2-ec2.jar -aws-android-sdk-1.2.2-s3.jar -aws-Android的SDK-1.2.2-debug.jar

我得到這個錯誤:

Could not find method com.amazonaws.services.ec2.AmazonEC2Client.describeRegions, referenced from method app.workman.Ec2Activity.onCreate.

這裏是我的活動代碼:

public class Ec2Activity extends Activity{ 
private AmazonClientManager acm; 
private AmazonEC2Client ec2; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ec2); 

    acm = new AmazonClientManager(); 

    if (this.acm.hasCredentials()){ 

    ec2 = acm.ec2(); 

    System.out.println(ec2.describeRegions()); 
    } 
    } 
} 

下面是代碼的AmazonClientMan ager.java:

public class AmazonClientManager { 
private static final String LOG_TAG = "AmazonClientManager"; 

private AmazonS3Client s3Client = null; 
private AmazonEC2Client ec2Client = null; 

public AmazonClientManager() { 
} 

public AmazonS3Client s3() { 
    validateCredentials(); 
    return s3Client; 
} 

public AmazonEC2Client ec2() { 
    validateCredentials(); 
    return ec2Client; 
} 

public boolean hasCredentials() { 
    return PropertyLoader.getInstance().hasCredentials(); 
} 

public void validateCredentials() { 
    if (s3Client == null || ec2Client == null) {   
     Log.i(LOG_TAG, "Creating New Clients."); 

//   AWSCredentials credentials = new BasicAWSCredentials(PropertyLoader.getInstance().getAccessKey(), PropertyLoader.getInstance().getSecretKey()); 
     AWSCredentials credentials = new PropertiesCredentials(this.getClass().getResourceAsStream("AwsCredentials.properties")); 

     s3Client = new AmazonS3Client(credentials); 
     ec2Client = new AmazonEC2Client(credentials); 
    } 
} 

public void clearClients() { 
    s3Client = null; 
    ec2Client = null; 
} 
} 

謝謝,問候。

+0

我覺得你得到這個例子在這裏亞馬遜演示項目amazons3client()函數是預定義函數u可以使用BasicAWSCredentials類此 –

回答

0

此代碼亞馬遜s3我試試這個工作。我加入AWS-Android的SDK-1.2.1-debug.jar這隻

public class AWSTestActivity extends ListActivity { 
/** Called when the activity is first created. */ 
AmazonS3Client s3Client; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    AWSCredentials credentials = new BasicAWSCredentials(
      "aKey", 
      "sKey"); 
    s3Client = new AmazonS3Client(credentials); 

    List buckets = s3Client.listBuckets(); 

    List<String> bucketNames = new ArrayList<String>(buckets.size()); 
    Iterator<Bucket> bIter = buckets.iterator(); 
    while (bIter.hasNext()) { 
     bucketNames.add((bIter.next().getName())); 
    } 

    List<String> ls=new ArrayList<String>(); 
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest(); 

    int keyCount = 1; 


     String path=""; 
      listObjectsRequest.setBucketName(bucketNames.get(0)); 
    //     listObjectsRequest.setPrefix(Key); 
      ObjectListing listing = s3Client.listObjects(listObjectsRequest); 
      List<S3ObjectSummary> objectSummaries1 = listing 
         .getObjectSummaries(); 
      ListIterator<S3ObjectSummary> listIterator = objectSummaries1 
         .listIterator(); 
       while (listIterator.hasNext()) { 
        path = listIterator.next().getKey(); 
        keyCount++; 
        ls.add(path); 
    //      if(folder.endsWith("_$folder$")){} 
    //      else{ 
    //        
//       ls.add(folder.replace(Key+"/", "")); 
    //      } 

    } 



    setListAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, bucketNames)); 



} 
protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Object o = this.getListAdapter().getItem(position); 
     String pen = o.toString(); 
     Toast.makeText(this, "Backet Name " + " " + pen, Toast.LENGTH_LONG).show(); 
     List<String> ls=new ArrayList<String>(); 
     ListObjectsRequest listObjectsRequest = new ListObjectsRequest(); 

     int keyCount = 1; 


      String path=""; 
       listObjectsRequest.setBucketName(pen); 
    //     listObjectsRequest.setPrefix(Key); 
       ObjectListing listing = s3Client.listObjects(listObjectsRequest); 
       List<S3ObjectSummary> objectSummaries1 = listing 
          .getObjectSummaries(); 
       ListIterator<S3ObjectSummary> listIterator = objectSummaries1 
          .listIterator(); 
        while (listIterator.hasNext()) { 
         path = listIterator.next().getKey(); 
         keyCount++; 
    //       ls.add(path); 
         if(path.endsWith("_$folder$")){ 
    //         ls.add(path.replace("_$folder$", "")); 
         } 
         else{ 

          ls.add(path.replace(path+"/", "")); 
         } 
         setListAdapter(new ArrayAdapter<String>(this, 
           android.R.layout.simple_list_item_1, ls)); 
     } 
        System.out.println("path::::::"+ls.toString()); 


     } 



} 
+0

謝謝爲答案,但沒有工作... 我得到這個錯誤:「05-08 11:22:56.914:E/AndroidRuntime(2463):java.lang.NoClassDefFoundError:com.amazonaws.auth.PropertiesCredentials」。 我現在只使用'aws-android-sdk-1.2.2-debug.jar'。 致謝問候。 – murilomsm

+0

同上BasicAWSCredentials: 「05-08 11:36:14.528:E/AndroidRuntime(2520):java.lang.NoClassDefFoundError:com.amazonaws.auth.BasicAWSCredentials」。 – murilomsm

+0

我無法理解...該jar在構建路徑中,但描述的類不被識別!我需要更多的幫助.... – murilomsm