android - 无法从 Android 模拟器连接到本地主机

标签 android django localhost httprequest

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

    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,可以有人看到问题了吗?

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

enter image description here

最佳答案

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

在你的 android-manifest 中

关于android - 无法从 Android 模拟器连接到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8301944/

相关文章:

iis - 如何在本地 IIS 中设置网站的主机名

android - 更新 Firestore 文档时出现 Coroutine JobCancellationException

android - Firebase Phone Authentication - 验证短信文本的内部化

JavaScript 代码在 localhost 中有效,但在实时站点中无效?

python - 限制wsgi中的系统资源

django - 包含或正在使用 DRF 进行过滤

PHPMyadmin 调用未定义函数 PMA_SQP_parse()

java - 按下按钮时创建复选框

android - 链完成同步和异步

django - 将多次南方移民压缩为一次移民