问题描述
在我的IIS我已经创建了网页API,它的项目链接的http://本地主机:54444
In my IIS i have created Web Api and it's Project Url is http://localhost:54444.
我ALBE从我在Windows8的浏览器连接到以下链接,但是我通过 http://10.0.2.2:54444 $ C我不能在我的Android模拟器浏览器这样做$ C>。
I am albe to connect to this Url from my browser in Windows8, however I am not able to do so in my android emulator browser via http://10.0.2.2:54444
.
我的Android浏览器能够连接到google.com。
My android browser is able to connect to google.com.
我也试图让使用Eclipse中的客户端库:
I am also trying to make an client in Eclipse using http://loopj.com/android-async-http/ library:
Log.v("bopzy_debug", "Testing HTTP Connectivity");
System.out.println("123");
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://10.0.2.2:54444/api/values/",
// client.get("http://google.pl",
new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Log.v("bopzy_debug", response);
}
@Override
public void onFailure(Throwable error, String content)
{
System.out.println("onFailure");
System.out.println(content);
Log.w("bopzy_debug", error);
}
@Override
public void onFinish() {
System.out.println("onfinish:");
Log.v("bopzy_debug", "Finished..");
}
});
}
在LogCat中信息我收到:
The LogCat info I'm getting:
07-18 09:18:17.471: V/bopzy_debug(1354): Testing HTTP Connectivity
07-18 09:18:17.471: I/System.out(1354): 123
07-18 09:18:17.621: I/System.out(1354): onFailure
07-18 09:18:17.644: I/System.out(1354): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
07-18 09:18:17.644: I/System.out(1354): <HTML><HEAD><TITLE>Bad Request</TITLE>
07-18 09:18:17.644: I/System.out(1354): <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
07-18 09:18:17.644: I/System.out(1354): <BODY><h2>Bad Request - Invalid Hostname</h2>
07-18 09:18:17.652: I/System.out(1354): <hr><p>HTTP Error 400. The request hostname is invalid.</p>
07-18 09:18:17.652: I/System.out(1354): </BODY></HTML>
07-18 09:18:17.652: W/bopzy_debug(1354): org.apache.http.client.HttpResponseException: Bad Request
07-18 09:18:17.652: W/bopzy_debug(1354): at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:235)
07-18 09:18:17.652: W/bopzy_debug(1354): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:79)
07-18 09:18:17.652: W/bopzy_debug(1354): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
07-18 09:18:17.652: W/bopzy_debug(1354): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
07-18 09:18:17.652: W/bopzy_debug(1354): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
07-18 09:18:17.652: W/bopzy_debug(1354): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-18 09:18:17.652: W/bopzy_debug(1354): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-18 09:18:17.652: W/bopzy_debug(1354): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-18 09:18:17.652: W/bopzy_debug(1354): at java.lang.Thread.run(Thread.java:856)
07-18 09:18:17.682: I/System.out(1354): onfinish:
07-18 09:18:17.682: V/bopzy_debug(1354): Finished..
我说:
<uses-permission android:name="android.permission.INTERNET" />
要AndroidManifest.xml文件。
to AndroidManifest.xml file.
任何建议将AP preciated。
Any suggestion will be appreciated.
推荐答案
的OP似乎已经找到了解决方案,但为后人的缘故,这里发生了什么,以及如何解决它更详尽的解释。
The OP seemed to have found a solution, but for posterity's sake, here's a more thorough explanation of what's happening and how to fix it.
我不得不使用本地服务器Rails的一个类似的问题。我意识到,我不明白的IP地址和主机名之间的关系。他们是不同的,如果我们不小心,我们能碰到的问题。
I had a similar issue using a local Rails server. I realized that I didn't understand the relationship between the IP address and the hostname. They're different and if we're not careful, we can run into issues.
主机名可以被分配到的IP地址,但IP地址没有被分配给主机名。
Hostnames can be assigned to the IP addresses, but IP addresses are not assigned to hostnames.
主机名 - > IP地址
hostname --> IP Address
这relatioship似乎微不足道,但要明白,这是一个单向的关系是非常重要的。主机名把你的IP地址,而IP地址是不知道主机名。
This relatioship seems trivial, but it's important to understand that it's a one way relationship. The hostname takes you to the IP address, but the IP address is not aware of the hostname.
假设我们有两个主机名指向127.0.0.1:localhost,且localdev。现在可以说,我有一个服务器侦听在localhost:3000。如果我在localdev键入:3000,你会期望发生
Lets say we have two hostnames that point to 127.0.0.1: localhost and localdev. Now lets say I have a server listening on localhost:3000. If I type in localdev:3000, what would you expect to happen?
临屋的答案是什么。
本地主机和localdev可能指向同一个IP地址,但IP地址不指向回主机名。有了这层关系,数据唯一无二的一种方式。
Localhost and localdev may be pointing to the same IP address, but the IP address doesn't point back to the hostnames. With this relationship, data only goes one way.
本地主机 - > 127.0.0.1&LT; - localdev
localhost --> 127.0.0.1 <-- localdev
我注意到,当我开始了Ruby on Rails的服务器,指定的日志服务器是听在localhost:3000。在我的浏览器,当我打本地主机:3000,我想看到的网站我建立。
I noticed that when I started a Ruby on Rails server, the logs specified that the server was listening on localhost:3000. In my browser, when I hit localhost:3000, I'd see the website I'm building.
现在本地主机,默认情况下,指向127.0.0.1,所以想如果我去127.0.0.1:3000发生什么呢?答:没有。 Ruby的服务器不说监听127.0.0.1,但本地主机。即使本地主机最终去为127.0.0.1,127.0.0.1是不是该服务器监听的地址。
Now localhost is, by default, pointing to 127.0.0.1, so what would happen if I went to 127.0.0.1:3000? Answer: nothing. The Ruby server does not say listening on 127.0.0.1, but localhost. Even though localhost eventually goes to 127.0.0.1, 127.0.0.1 is not the address that the server is listening on.
现在,如果服务器监听127.0.0.1,你从你的浏览器中点击本地主机,你仍然会得到你的网站,因为本地主机都转发到127.0.0.1。必然,如果你尝试打另一个主机名指向127.0.0.1(如localdev是从previous例子),这也将正常工作。
Now, if the server was listening on 127.0.0.1 and you hit localhost from your browser, you will still get your website because localhost forwards everything to 127.0.0.1. Consequentially, if you try to hit another hostname pointing to 127.0.0.1 (such as localdev was from the previous example), that will also work.
一旦我们理解本地主机和127.0.0.1之间的关系,我们可以更好地理解10.0.2.2之间从Android的到来,本地主机机器上的关系。在说10.0.2.2链接到模拟器:
Once we understand the relationship between localhost and 127.0.0.1, we can better understand the relationship between 10.0.2.2 coming from Android and localhost on your machine. The Android docs on Using the Emulator says that 10.0.2.2 links to:
特殊别名主机回环接口(即127.0.0.1在开发机器上)
请注意,它指向的IP地址,而不是主机名。如果我们对127.0.0.1的而不是本地主机服务器监听,仿真器将能够成功地将消息发送到我们的服务器。
Notice that it points to the IP address, not the hostname. If we had the server listen on 127.0.0.1 instead of localhost, the emulator will be able to successfully send messages to our server.
这篇关于试图从Android模拟器连接到国际空间站7防爆preSS时坏请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!