[英]How to test service returning JSON on localhost with android emulator
I've got a dropwizard service running on localhost:8080. 我有一个在本地主机上运行的dropwizard服务:8080。
It returns the following json on this url: http://localhost:8080/users/johndoe/items
它在此URL上返回以下json: http://localhost:8080/users/johndoe/items
[
{
"id": 1,
"name": "Item1"
},
{
"id": 2,
"name": "Item 2"
},
{
"id": 7,
"name": "Item 3"
},
{
"id": 8,
"name": "Item4"
}
]
When I try to access this URL from my android emulator I am getting this exception: 当我尝试从Android模拟器访问此URL时,出现此异常:
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
In my manifest I've given internet permissions 在清单中,我已授予互联网权限
<!-- Permission - Internet Connect -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I've also tried to change localhost
to 127.0.0.1
to no avail 我也尝试将localhost
更改为127.0.0.1
无济于事
Question 题
I understand that the API will need to be put on the server accessible by the internet for production app but for development purposes what is the way to resolve this? 我知道该API需要放置在可通过互联网访问的用于生产应用的服务器上,但是出于开发目的,解决该问题的方法是什么?
Localhost refers to the address of the emulator. Localhost是指仿真器的地址。 If you want to access the computer which runs the emulator use 10.0.2.2
, which is the default ip address of the host machine. 如果要访问运行仿真器的计算机,请使用10.0.2.2
,这是主机的默认IP地址。
If you want to test your server on localhost from an Emulator, you have two options : 如果要通过仿真器在本地主机上测试服务器,则有两个选择:
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.