views:

272

answers:

3

Hello, I'm quite new to android and try to implement http client to communicate to a REST server. I'm starting with the Get method but I have some problems using android 2.0 on the emulator.

String url = "http://www.google.fr/search?q=android"
HttpClient client = new DefaultHttpClient(new BasicHttpParams());
HttpGet getMethod = new HttpGet((url));
HttpResponse httpResponse = client.execute(getMethod);
HttpEntity = httpResponse.getEntity());
int length = (int) httpEntity.getContentLength();
=> length is -1 :-(

A look into the logs gives me the UnresolvedHostException.

I searched on google and found several guys having the same problem but who didn't find any answer. Is there a particuliar way to configure the android emulator to have this working ? Just to precise, I have added the Internet access in the android manifest.

Thanks a lot for your help. Luc

+2  A: 

First, try accessing that page from the Browser application. If that fails, check your firewall or deactivate your proxy server.

Once the Browser can access the page, try getting rid of the new BasicHttpParams() (it is not needed) and see if that helps. Otherwise, what you have seems OK.

CommonsWare
+1Apart from disabling the proxy, you can alternatively also proxy-enable the DefaultHttpClient instance.
Samuh
Thanks a lot, I will try this, I guess the emulator is somehow different from the real condition, am i right ?
Luc
Sorry for my late reply.I can access the URL from the browser (http://www.google.fr/search?q=toto), that works fine. But this is still not working through the code (with or without the 'new BasicHttpParams()').My searches on google also pointed out this could be because of DNS problem. One of the solution proposed was to setup a dns server and run the emulator specifiying the url of this server. If this is the case I still cannot understand with this is working in the browser... I am kind of lost here :(Thanks a lot,Luc
Luc
Try editing the issue, putting in your manifest and where you're seeing the UnresolvedHostException.
CommonsWare
A: 

If you are sitting behind a proxy, you can alternatively set Proxy IP/port in your DefaultHttpClient instance. The method to do this would look something like:

public void setProxy(DefaultHttpClient httpclient) {     
           final String PROXY_IP = "<insert your IP here>";     
            final int PROXY_PORT = <insert_PROXY_PORT#>;     

            httpclient.getCredentialsProvider().setCredentials(     
                    new AuthScope(PROXY_IP, PROXY_PORT),     
                    new UsernamePasswordCredentials(     
                            "username", "password"));     

           HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);     

           httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,     
                   proxy);     


       }

However, note that if you are running your code on the phone(which wont have any such proxy settings) you will have to disable this function;
On phone, your existing code should work just fine.

Samuh
Hello,thanks for your reply. No, I'm not behind a proxy. I will try to test my code on a real phone then.Thanks a lot.
Luc
A: 

Try URL without any GET-Parameter (?q=android).

String url = "http://www.google.fr/search";

I think the URLParams have to be set in another way?

mg
Hello, because it is an HttpGet method, I think the parameters must be in the url. In fact, this is working fine now (see my comment in the accepted answer). Thanks, Luc
Luc