【问题标题】:Can't connect to local host form android emulator无法从 android 模拟器连接到 localhost
【发布时间】:2012-01-08 06:32:02
【问题描述】:

所以我有一个 django 服务器在我的本地机器上运行,我想通过我在模拟器中运行的 android 应用程序连接到该服务器。当我在网络浏览器中输入它时一切正常(我得到了我期待的 json 对象)。但我的安卓代码不工作。

    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;

我尝试将localhost127.0.0.1 替换为10.0.2.2,并且我还尝试了端口8000 而不是8080,有人能看出问题吗?

这是堆栈跟踪:(如果您在新选项卡中打开它可以阅读它)

【问题讨论】:

  • 没有。但是你可以。请发布堆栈跟踪:)
  • 永远不要在电脑上设置服务器并用它来测试模拟器中的应用程序,但是:在这种情况下,127.0.0.1 等不会将手机/模拟器称为本地主机,而不是您尝试访问的主机?
  • 你在网络浏览器中输入了什么(准确的)?
  • 就我而言,我使用的是 192.168.1.X
  • http://127.0.0.1:8000/getUserInfo/willyb/http://localhost:8000/getUserInfo/willyb/ 都可以工作,我在那个代码中都试过了

标签: android django localhost httprequest


【解决方案1】:

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

在你的 android 清单中

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2018-08-21
    • 2016-05-28
    • 2017-08-11
    • 2019-03-08
    • 2017-07-25
    • 2022-06-14
    相关资源
    最近更新 更多