Android Connect Node.js REST流结束 [英] Android connect Node.js REST unexpeceed end of stream

查看:80
本文介绍了Android Connect Node.js REST流结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个带有Express的简单Node.js本地服务器,该服务器返回了这样的json

I have setup a simple Node.js local server with express that returns a json like this

    router.get('/addUser', function(req, res, next) {
    res.send('{ "result" : "FAILED", "error" : "TEST" }');
});

Node.js 侦听localhost端口3000,因此当连接到 localhost:3000/addUser 时,我希望取回此json {"result":"FAILED","error":"TEST"}

Node.js listens to localhost port 3000, so when connect to localhost:3000/addUser I expect to get back this json { "result" : "FAILED", "error" : "TEST" }

当我尝试使用 HttpURLConnection 从Android Emulator调用上述URL到 10.0.2.2:3000 (本地主机的Android IP)时,出现异常:* java.io.IOException:Connection {10.0.2.2:3000,代理= DIRECT @ hostAddress = 10.0.2.2 cipherSuite = none protocol = http/1.1}(回收计数= 0)上流的意外结束.*

When I try to call the above url from Android Emulator to 10.0.2.2:3000 (Android IP for localhost) with HttpURLConnection I get an exception :*java.io.IOException: unexpected end of stream on Connection{10.0.2.2:3000, proxy=DIRECT@ hostAddress=10.0.2.2 cipherSuite=none protocol=http/1.1} (recycle count=0).*

使用浏览器或邮递员在以下位置测试我的node.js服务器时 http://localhost:3000/addUser 会正确返回json对象.

When using a browser or postman to test my node.js server at http://localhost:3000/addUser it returns the json object correctly.

当我尝试从Android与本地Apache服务器通信到返回相同json的页面时,它可以正常工作.

When I try to communicate from Android to a local Apache Server to a page returning the same json it works correctly.

从Android模拟器连接到本地Node.js服务器时,出现了问题.

Something goes wrong when conenecting from Android emulator to local Node.js server.

推荐答案

最后,问题是由查询字符串中的空格引起的.浏览器和邮递员似乎可以检测并自动更正此错误.

Finally the problem was caused by a space in querystring. Browser and Postman seem to detect and automatically correct this error.

这篇关于Android Connect Node.js REST流结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆