【问题标题】:NativeScript, Android - connection to localhost failsNativeScript,Android - 与本地主机的连接失败
【发布时间】:2018-08-31 14:09:44
【问题描述】:

我在我的 android 模拟器上连接到 localhost 时遇到问题。 我已经创建了休息节点服务器:

...
app.listen(80, () => console.log('Example app listening on port 80!'))

当我尝试使用 NativeScript 连接到它时:

this.http.get("10.0.2.2/api/categories")
        .map(function (result) { return result.json(); })
        .subscribe(function (result) {
        console.log(result);
    });

我明白了:

System.err: java.net.MalformedURLException: no protocol: 10.0.2.2/api/categories
System.err:     at java.net.URL.<init>(URL.java:590)
System.err:     at java.net.URL.<init>(URL.java:487)
System.err:     at java.net.URL.<init>(URL.java:436)
System.err:     at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:512)
System.err:     at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
System.err:     at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err:     at java.lang.Thread.run(Thread.java:764)
JS: ERROR Error: Uncaught (in promise): Response with status: 200  for URL: null

我错过了什么吗?

【问题讨论】:

  • 你试过 localhost/api/categories 吗?

标签: android node.js http localhost nativescript


【解决方案1】:

试试这个:

this.http.get("http://10.0.2.2/api/categories")
        .map(function (result) { return result.json(); })
        .subscribe(function (result) {
        console.log(result);
    });

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2021-11-27
    • 2017-12-24
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多