0

我是 Android 新手,我需要开发一个操作 EC2 和 S3(亚马逊网络服务)的应用程序。我在 VirtualBox 上的 Ubuntu 10.04 上使用 Eclipse Indigo。我已经安装了适用于 Android 的 SDK,创建了一个新项目,添加了这些 jar:

-aws-android-sdk-1.2.2-ec2.jar -aws-android-sdk-1.2.2-s3.jar -aws-android-sdk-1.2.2-debug.jar

我收到了这个错误:

找不到方法 com.amazonaws.services.ec2.AmazonEC2Client.describeRegions,引用自方法 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());
    }
  }
}

这是 AmazonClientManager.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;
}
}

感谢和问候。

4

1 回答 1

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());


        }



 }
于 2012-05-08T07:24:44.180 回答