在我的 IIS 中,我创建了 Web Api,它的项目 URL 是http://localhost:54444.
我可以从 Windows8 中的浏览器连接到这个 Url,但是我无法在我的 android 模拟器浏览器中通过http://10.0.2.2:54444
.
我的 android 浏览器能够连接到 google.com。
我还尝试使用http://loopj.com/android-async-http/库在 Eclipse 中创建客户端:
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 信息:
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 文件。
任何建议将不胜感激。