简体   繁体   中英

How can I consume a JSON web service with Restlet?

I'm trying to consume a restful web service with Restlet. The service is giving me JSON, so I'm using a JsonRepresentation object to get it, but it's not working. I'm getting a null pointer exception when I call jsonRepresentation.getJsonObject() .

Here is my code:

ClientResource resource = 
    new ClientResource("https://api.prosper.com/api/Listings?$top=3");
resource.setChallengeResponse(
    ChallengeScheme.HTTP_BASIC, 
    "username", 
    "password");
try {
    JsonRepresentation jsonRepresentation = 
        new JsonRepresentation(resource.getResponse().getEntity());
    jsonRepresentation.getJsonObject();
} catch (Exception e) { }

Any idea what the issue could be or how I could trouble shoot this?

I think that you missed the call to the service:

ClientResource resource = (...)
(...)
Representation repr = resource.get();
JsonRepresentation jsonRepresentation = new JsonRepresentation(repr);
JSONObject jsonObj = jsonRepresentation.getJsonObject();

Hope it helps you. Thierry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM