Reputation: 546
I've been playing around a bit with restlet, and so far I like it :)
I am having some trouble getting a restlet client to work on an android emulator, though.
Right now a have a very simple restlet server running under JSE. All it does is expose a root resource (which for the moment is just a line of text) via the GET method. I can get consume the resource with a browser, and also with a simple restlet client running in the console. I have modified the code for the JSE client to try to connect from an android emulator but I can't get it to work:
package mypackage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.restlet.resource.ClientResource;
import org.restlet.representation.Representation;
public class MyActivity extends Activity {
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
ClientResource res = new ClientResource( "http://server-ip:8111/" );
Representation rep = res.get();
TextView tv = new TextView( this );
try {
tv.setText( rep.getText() );
} catch ( Exception ex ) {
tv.setText( "Exception: " + ex.getMessage() );
}
setContentView( tv );
}
}
Notes:
(1) The built-in browser on the android emulator can connect to the web service and retrieve the text.
(2) The project Manifest.xml file has been modified to enable the uses-permission INTERNET.
(3) The following client using the Apache HttpClient library that comes bundled with android works just fine:
package mypackage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet( "http://server-ip:8111/" );
ResponseHandler<String> handler = new BasicResponseHandler();
TextView tv = new TextView( this );
try {
String content = client.execute( get, handler );
client.getConnectionManager().shutdown();
setContentView( tv );
tv.setText( content );
} catch ( Exception ex ) {
tv.setText( "Exception: " + ex.getMessage() );
}
}
}
Any ideas what I might be doing wrong?
Thanks in advance,
Mike.
Upvotes: 1
Views: 1877
Reputation: 2892
One workaround is to register the org.restlet.ext.httpclient extension manually to replace the internal/default HTTP client. See Restlet user guide: http://wiki.restlet.org/docs_2.1/13-restlet/275-restlet/266-restlet.html
Recent Restlet 2.1 snapshots have also fixed a long standing issue with the internal HTTP connector that could cause unexpected timeouts.
Upvotes: 2