我在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
我错过了什么吗?
答案 0 :(得分:1)
试试这个:
this.http.get("http://10.0.2.2/api/categories")
.map(function (result) { return result.json(); })
.subscribe(function (result) {
console.log(result);
});