0

I am currently using Restlet 2.1.2 on Android, and I'd like to know how to reuse the instance Clientresource in my application.

Here is the code I use:

Client client = new Client (new Context (), Protocol.HTTP);
     ClientResource clientResource ClientResource = new ("http://codeblow.com/");
     clientResource.setNext (client);
     / / Executors.newCachedThreadPool ExecutorService pool = ();

     clientResource.get ();
     clientResource.release ();

     ClientResource clientResource1 ClientResource = new ("http://codeblow.com/");
     clientResource.setNext (client);

"Starting the internal [HTTP/1.1] client" "Starting the internal [HTTP/1.1] client"

As you can see, at each new instantiate of ClientResource, it create new clients. I just want to keep 1 clientresource and then make few requests with the same instance to the server.

Whats is the best way to do that ?

Other point: I used the #setNext() method to keep the client, but i feel that's useless., because it create another client anyway.

Best regards

2 Answers2

0

You should remove the call to release() which has for effect to stop the client connector, which is then auto-started again by the second client resource.

Jerome Louvel
  • 2,882
  • 18
  • 19
0

This is how I solved this issue in my android app using restlet 2.3.1. I created a Reference for the url, segment and parameters that I wanted to access. Then used .setReference() on the ClientResource. That way you can create one instance of the ClientResource and use .setReference() repeatedly to change the parameters. It will only start and stop the HTTP client once with this approach.

Below is a code fragment that shows the idea. This code was using an IntentService and JSON responses, but those are not required. I didn't include the code for the Resource objects because it's not directly related to the question.

import org.restlet.data.Reference;
import org.restlet.engine.Engine;
import org.restlet.ext.jackson.JacksonConverter;
import org.restlet.resource.ClientResource;

public class MyDataService extends IntentService {
    private static final String CLOUD_DATA_ENDPOINT = "http://my.server-app.com";
    private ClientResource cr = null;

    protected void onHandleIntent(Intent intent) {
        cr = getClientResource();

        UserObj = retrieveUser("myuserid");
        AccountObj = retrieveAccount("myacctid");

        cr.release();
    }

    private ClientResource getClientResource() {
        Engine.getInstance().getRegisteredConverters()
                .add(new JacksonConverter());

        // Initialize the resource proxy.
        ClientResource cr = new ClientResource(CLOUD_DATA_ENDPOINT);

        // Workaround for GAE servers to prevent chunk encoding
        cr.setRequestEntityBuffering(true);
        cr.accept(MediaType.APPLICATION_JSON);

        return cr;
    }

    private UserObj fetchUser(String userID){
        Reference queryRef = new Reference(CLOUD_DATA_ENDPOINT);
        queryRef.addSegment("user");
        queryRef.addQueryParameter(QueryResource.FIELD_USER_ID, userID);
        cr.setReference(queryRef);

        UserResource resource = cr.wrap(UserResource.class);

        // may return null or empty object
        return resource.retrieve();
     }

    private AccountObj fetchAccount(String accountID){
        Reference queryRef = new Reference(CLOUD_DATA_ENDPOINT);
        queryRef.addSegment("account");
        queryRef.addQueryParameter(QueryResource.FIELD_ACCOUNT_ID, accountID);
        cr.setReference(queryRef);

        AccountResource resource = cr.wrap(AccountResource.class);

        // may return null or empty object
        return resource.retrieve();
    }
}
Doug B
  • 21
  • 2