-1

I am trying to access my uri from the below code

    AsyncTask asyncTask = new AsyncTask() {

        @Override
        protected Object doInBackground(Object... params) {
            AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
            HttpUriRequest uriGetRequest = new HttpGet("http://localhost:8080/restsql-0.8.6/res/");
            HttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(uriGetRequest);
                ;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                if(e!=null){
                Log.e("ModelLoader", e.getMessage(), e);
                }
                Log.e("Model Loader", "Error while calling execute");
            }
            return httpResponse;
        }
    };
    asyncTask.execute(new Object());

But always the httpresponse is null. Even no exception is thrown. I have checked with a valid uri like https://google.co.in/ and its working fine. Also my URI from browser is working fine . As there is no exception thrown I am not able to proceed further.

flx
  • 14,146
  • 11
  • 55
  • 70
saurav
  • 5,388
  • 10
  • 56
  • 101

2 Answers2

2

In AVD to connect to localhost you need to use url

       http://10.0.2.2/

instead of

        http://localhost/

For reference.

Nirmal
  • 2,340
  • 21
  • 43
  • 2
    +1, `localhost` on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface, you should use the special address `10.0.2.2` instead – Tarsem Singh Sep 16 '13 at 08:22
  • Why it shows me 400: Bad request... my services running on localhost. and I am calling it from android emulator with path : http://10.0.2.2:52511/api/Values/GetProduct/5 – Keval Patel Jul 26 '15 at 06:00
0

To access localhost in emulator use 10.0.2.2

http://localhost:8080/restsql-0.8.6/res/

should be

http://10.0.2.2:8080/restsql-0.8.6/res/

Check the docs

Sunil Mishra
  • 3,796
  • 1
  • 27
  • 40