3

I'm working with Amazon SimpleDB and attempting the creation of a DB using the following tutorial . Basically it throws an error i.e. Error occured: java.lang.String cannot be cast to org.apache.http.HttpHost. The full stacktrace is as below:

Error occured: java.lang.String cannot be cast to org.apache.http.HttpHost java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.http.HttpHost at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:416) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) at com.xerox.amazonws.common.AWSQueryConnection.makeRequest(AWSQueryConnection.java:474) at com.xerox.amazonws.sdb.SimpleDB.makeRequestInt(SimpleDB.java:231) at com.xerox.amazonws.sdb.SimpleDB.createDomain(SimpleDB.java:155) at com.amazonsimpledb.SDBexample1.main(SDBexample1.java:19)

My code is as below (note i have substituted the AWS access id and secret key with the actual values):

public static void main(String[] args) {

     String awsAccessId = "My aws access id";
     String awsSecretKey = "my aws secret key";

     SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);

     try {
        Domain domain = sdb.createDomain("cars");

        System.out.println(domain);

    } catch (com.xerox.amazonws.sdb.SDBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Any ideas as to why the above mentioned error is occurs.

I appreciate any assistance.

2 Answers 2

7

It seems you are using the Typica client library, which is pretty much unmaintained since mid 2011, see e.g. the rare commmits and the steady growing unresolved issues, where the latest one appears to be exactly yours in fact, see ClassCastException using Apache HttpClient 4.2:

  • According to the reporter, things appear to be functional once we downgrade back to Apache HttpClient 4.1, so that might be a temporary workaround eventually.

Either way I highly recommend to switch to the official AWS SDK for Java (or one of the other language SDKs), which isn't only supported and maintained on a regular fashion, but also closely tracks all AWS API changes (admittedly this isn't that critical for Amazon SimpleDB, which is basically frozen technology wise, but you'll have a much easier time using the plethora of AWS Products & Services later on).

The SDK includes a couple of samples (also available via the Eclipse Toolkit wizard), amongst those one for SimpleDB - here's a condensed code excerpt regarding your example:

BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(
    awsAccessId, awsSecretKey);
AmazonSimpleDB sdb = new AmazonSimpleDBClient(basicAWSCredentials);
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
sdb.setRegion(usWest2);

try {
    // Create a domain
    String myDomain = "MyStore";
    System.out.println("Creating domain called " + myDomain + ".\n");
    sdb.createDomain(new CreateDomainRequest(myDomain));

    // ...
    // Delete a domain
    System.out.println("Deleting " + myDomain + " domain.\n");
    sdb.deleteDomain(new DeleteDomainRequest(myDomain));
} catch (AmazonServiceException ase) {
    // ...
} catch (AmazonClientException ace) {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@AMonari - if you tried send me a comment, it has been truncated somehow. I've updated my answer with details regarding the unmaintained/unresolved upstream issues, where the latest one appears to be exactly yours in fact, see issue 135. I think this issue and the apparent lack of maintainer response pretty much confirms the point I'm trying to make, namely that you can't expect any fixes within this library anymore and would need to do it yourself - but why should you, given the existing superior alternative?
0

Please try to create instance of SimpleDB with server and port and let me know if it works.

public SimpleDB objSimpleDB = null;
private String awsAccessKeyId = "access key";
private String awsSecretAccessKey = "secret key";
private boolean isSecure= true;
private String server = "sdb.amazonaws.com";
private int port=443;

try{
SimpleDB objSimpleDB = new SimpleDB(awsAccessKeyId, awsSecretAccessKey, isSecure, server, port);
Domain domain = objSimpleDB .createDomain("cars");
} catch (com.xerox.amazonws.sdb.SDBException e) {
      //handle error
}

3 Comments

Hi Ashish, thanks for the assistance. I attampted your suggestion above and I'm still getting the same error i.e. Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.http.HttpHost with the same same stacktrace. Any ideas why? thanks
@AMonari - I've just updated my answer regarding the apparently identical upstream issue 135 (ClassCastException using Apache HttpClient 4.2), which seems to indicate this being caused by a incompatibility introduced when using Apache HttpClient 4.2, while the library still worked with HttpClient 4.1.
@Steffen Opel - firstly i really appreciate your contribution. I've been off this matter but I'm back on it. I get your point regrading the better official AWS SDK for Java and will attempt utilising this and I'll see how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.