Hi, Note that creating an httpClient with new DefaultHttpClient() fails to connect with some mobile carrier, for example Orange in France. (ERR_CONNECT_FAIL)
I recommend creating your httpClient using a more robust method as described here : http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/ Best regards Reda On 1 sep, 22:29, authorwjf <[email protected]> wrote: > In its most basic format, calling a rest service is simply doing an > HTTP call. For example: > > public void callSomeApi() throws Exception > { > DefaultHttpClient httpclient = new DefaultHttpClient(); > > httpclient.getCredentialsProvider().setCredentials( > new AuthScope("api.service.domain.com", 443), > new UsernamePasswordCredentials("username", > "password")); > > HttpGet httpget = new HttpGet("https://api.service.domain.com/ > rest/entry_point"); > > ResponseHandler<String> responseHandler = new > BasicResponseHandler(); > String responseBody = httpclient.execute(httpget, > responseHandler); > > //REST APIs typically return JSON or XML > JSONArray response = new JSONArray(responseBody); > > int items = response.length(); > > ListView list = new ListView(this); > > for (int i = 0; i < items; i++) { > ListItem item = new ListItem(); > item.label = > response.getJSONObject(0).getString("item_you_were_looking_for"); > > } > > httpclient.getConnectionManager().shutdown(); > > } > > This is a pretty bare-bones HTTP example, and if suffers from two > problems. The first is it's not very robust. > This you can get around if you are semi-competent by adding error > handling and whatever else. The second, > is that on the android platform these sorts of long running I/O > operations need to happen on their own thread. > That's fine and dandy so long as nothing interrupts the thread, like > an orientation change or an incoming call, > but we all know that is exactly what happens on a phone. > > For me a workable solution was to create a service in the start up > activity of my application. So when someone > starts my app, the onCreate method first starts the backgroundIO > service, and then starts the first > UI activity. > > /** > * This is the standard Android on create method that gets called > when the activity initialized. > */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > //Start up background I/O service > Intent svc = new Intent(this, BackGroundIO.class); > startService(svc); > //Start up first UI > Intent i = new Intent(this, MyFirstUI.class); > i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); > startActivityForResult(i, 1); > } > > So now the question is what is BackGroundIO, and how does it interact > with the UI? In my case the background i/o > class was simply a serialized request queue with static members for > adding / getting requests. In other words the class > had a couple members for adding requests, retrieving them, and > clearing any pending I/O. > > public class BackGroundIO extends Service{ > > private static Thread mWorker = null; > > public static long addToQue(ApiRequest request) { > //mechanism for adding requests > //ApiRequest is some custom class that defines all the > elements you need to make your api calls > //Probably username, password, entrypoint, and a UI runnable > or handler for signaling on completion > //return a unique request identifier to the UI > } > > public static ApiRequestResult getResult(long identifier) throws > RequestNotFoundException, RequestStillPendingException { > //mechanism for returning a result from memory or file > //return the ApiRequestResult for some unique identifier if > found > //if it came from disk don't forget to delete it from the file > } > > public static void clearQue() { > //mechanism for clearing any and pending requests > //usually called on shutdown > } > > @Override > public IBinder onBind(Intent arg0) { > // Not using IPC so just return empty > return null; > } > > @Override > public void onCreate() { > super.onCreate(); > mWorker = new Thread() { > public void run() { > while (true) { //this is my main looper > //Is there a pending request? > //Yes > // 1 CallSomeApi function shown above. > // 2 Remove the request from the queue of > pending requests > // Is the UI Activity who made this request > still around? > // YES > // 1 Signal the UI the result is ready > // NO > // 1 Save the result to disk and let a UI > retrieve it later when he wants / can. > //No > //Sleep for a short bit > } > }; > mWorker.start(); > } > } > > } > > As you can see this is again overly simplified and I'm typing it in a > text editor so there may be a syntax issue > here and there as well. But hopefully you get the idea and this helps > you start out on the right track. I too > attended the Google Session in CA and still had a number of false > starts. My solution may not be the best one > as my background prior to working on Android was device drivers but my > product has been in the market for several > months now and it seems to be performing well under real world > conditions. > > Good luck to you! -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

