0

(on the emulator) I have an application that is making get request to a .NET web API.

HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://10.0.2.2:64466/api/Products/1");
        String text = null;
        try {  
HttpResponse response = httpClient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            text = getASCIIContentFromEntity(entity);
        } catch (Exception e) {
            return e.getLocalizedMessage();
        }
        return text;
    }

The API is made of ASP.net web application - web API project template, and it is currently running - I haven't post it on IIS(the url localhost:64466/api/Products/1 works correctly), but when I run the android app, it get an error - error 400 the request hostname is invalid.Maybe this is totally wrong way of approaching this, I am confused - would you kindly show me the error in the implementation or the tell me if the way I am doing it is totally wrong?

Tomek
  • 410
  • 3
  • 7
  • 16
  • try to open http://10.0.2.2:64466/api/Products/1 from browser of emulator and post result – MrDumb Mar 23 '15 at 10:22
  • Use [this way](http://pastebin.com/rB8V0CS2), if you are using get Method – MrDumb Mar 23 '15 at 10:30
  • well - "the website is unavailable" but when the address is localhost:64466/api/products/1 the error is connection to localhost:64466 refused. I followed http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips - this advice, and http://developer.android.com/tools/devices/emulator.html#emulatornetworking info – Tomek Mar 23 '15 at 10:38
  • your emulator is not able to access the website that is why you are getting this error. Try port 8080 – MrDumb Mar 23 '15 at 10:44
  • I am sorry, but I don't understand how would it work - there is no api on this 8080 port, I cannot access it in the browser. – Tomek Mar 23 '15 at 11:10
  • http://10.0.2.2:8080/api/Products/1 check from your emulator browser if it is able to access service – MrDumb Mar 23 '15 at 11:12
  • `but when I run the android app`. You did not tell if you run your app on an emulator or on a device. Your code assumes running on an emulator with the emulator on the pc where your webserver is running. – greenapps Mar 23 '15 at 11:47
  • sorry - I run the app on emulator. – Tomek Mar 23 '15 at 12:08
  • Already twice you were asked to try the used url in a browser running on the emulator. You did not respond. Please do as it gives valuable info. – greenapps Mar 23 '15 at 12:52
  • I am sorry, I couldn't respond earlier - when I access 10.0.2.2:8080/api/Products/1 from the browser of emulator - I get err_connection_time_out. I checked all the combinations- with my ip instead of locoalhost, I checked 8080 and 64466 machine. The only promising result is 10.0.2.2:64466/api/Products/1 - I get 400 wrong hostname. – Tomek Mar 23 '15 at 17:34
  • ok, so I host the service on iis, it's working now. Apparently there is no way to access the service from emulator, if it is not hosted. sorry for bothering you. – Tomek Mar 24 '15 at 10:01

0 Answers0