2

I'm trying to make a GET request to a ASP.NET Web API from Android. The API should return a JSON object with some users. When calling the API from the browser everything works fine, but when attempting to call from the Androind application nothing happens. I've looked into Fiddler and no requests are sent.

This is the code I use to make the call:

class GetData extends AsyncTask<String, String, String>{
        @Override 
        protected String doInBackground(String... params){
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            try{
            HttpGet get = new HttpGet("http://10.0.2.2:38216/api/user");
            response = client.execute(get);
            if(response != null){
                InputStream inStream = response.getEntity().getContent();
                String message = convertToString(inStream);
                return message;
            }
        }
        catch(Exception exp){
            Log.d("Error message!", exp.toString());
        }
        return null;
    }
}

Any ideas or hints on what I'm doing wrong would be appreciated!

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
Cosmin Ionascu
  • 7,018
  • 5
  • 25
  • 42
  • Just to confirm, you are running this on an emulator, and your Web app is listening on `localhost` of the development machine running that emulator? If yes, did LogCat show an exception and stack trace? Also, are you sure that you are executing this task? – CommonsWare Jul 16 '13 at 12:24
  • I am running from an emulator and LogCat didn't show anything. Also I'm sure the task gets called because I've looked into the debugger and the breakpoint on the task gets hit. Also `client.execute(get)`is called but response comes back with nothing useful and Fiddler doesn't show any call to the Web API. – Cosmin Ionascu Jul 16 '13 at 12:27
  • "with nothing useful" -- if it did not throw an exception, then there is something useful in the response, such as status codes. – CommonsWare Jul 16 '13 at 12:28
  • Ok so I've looked again in the debug and I get a status code 200. Which tells me that the call was done Ok. But I took another peek into Fiddler and nothing is shown there. Why when making the call from the browser I see it but when I make the call from the Android app nothing appears? Am I missing something ? – Cosmin Ionascu Jul 16 '13 at 12:34
  • "Am I missing something ?" -- yes, you are missing output in Fiddler. :-) Is Fiddler running on port `38216`? Or did you set up Fiddler as a proxy in your Web browser, and port `38216` is the Web app itself? – CommonsWare Jul 16 '13 at 12:36
  • I'm monitoring all traffic in Fiddler and port `38216` is the Web API itself (that's the port IIS Express assigned to it). – Cosmin Ionascu Jul 16 '13 at 12:37

1 Answers1

0

HttpClient does not automatically use a proxy that you may have configured in some other app (e.g., Web browser). You will need to teach HttpClient about that, either via DEFAULT_PROXY or via System.setProperty() for http.proxy* or https.proxy* (in your case, http.proxy*, as you are not using SSL).

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Well I'm getting the data now. But the Fiddler problem is still there. It's not showing me the request. I can live with it because I now know that the call is being made, but it still bugs me because I can't see the request in Fiddler. Can you point me to why this is happening? – Cosmin Ionascu Jul 16 '13 at 12:54
  • @CosminIonascu: Beats me. If you have the proxy settings pointing at Fiddler, I have no idea how you would be getting the data and Fiddler not be involved in the process. – CommonsWare Jul 16 '13 at 13:48
  • This question may help with your Fiddler problem: http://stackoverflow.com/questions/214308/how-do-i-get-fiddler-to-stop-ignoring-traffic-to-localhost – Jon Susiak Jul 16 '13 at 14:44