wfbarksdale
wfbarksdale

Reputation: 7606

Can't connect to local host form android emulator

So I have a django server running on my local machine that I want to connect to through an android app that I am running in the emulator. Everything works fine when I type this into the web browser (i get the json object I am expecting back). But my android code is not working.

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://10.0.2.2:8080/getUserInfo/" + username + "/"));

        //next line throws exception
        HttpResponse response = client.execute(request);
        HttpEntity resEntity = response.getEntity();  
        if (resEntity != null) {  
             Log.i("GET RESPONSE",EntityUtils.toString(resEntity));
             String jsonString = EntityUtils.toString(resEntity);
             return jsonToAccount(jsonString);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

I have tried subbing in localhost and 127.0.0.1 for 10.0.2.2 and I have also tried port 8000 instead of 8080, can anybody see the problem?

Here is the stack trace: (you can read it if you open it in a new tab)

enter image description here

Upvotes: 2

Views: 3590

Answers (1)

vanleeuwenbram
vanleeuwenbram

Reputation: 1339

put

<uses-permission android:name="android.permission.INTERNET" />

in your android-manifest

Upvotes: 9

Related Questions